list vs. tuple [Re: len() should always return something]
In article mailman.3674.1248461573.8015.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: Better:if isinstance(x, (int, float, complex)): I never noticed this before, but it seems odd that the second argument to isinstance() should be a tuple. Using the normal arguments made about tuples vs. lists, it seems like a list would be the right data structure here. I suppose a set would be even more right, but (I'm pretty sure) isinstance() predates sets. I'm curious why a tuple was chosen. -- http://mail.python.org/mailman/listinfo/python-list
Re: list vs. tuple [Re: len() should always return something]
On Fri, 24 Jul 2009 15:03:29 -0400, Roy Smith wrote: In article mailman.3674.1248461573.8015.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: Better:if isinstance(x, (int, float, complex)): I never noticed this before, but it seems odd that the second argument to isinstance() should be a tuple. Using the normal arguments made about tuples vs. lists, it seems like a list would be the right data structure here. What would be the point of using a list? You're never going to sort it, or append items to it, or otherwise mutate it. You build it, pass it to a function which doesn't modify it in any fashion, then it gets garbage collected. I suppose a set would be even more right, but (I'm pretty sure) isinstance() predates sets. Yes. [st...@sylar ~]$ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam isinstance built-in function isinstance set Traceback (innermost last): File stdin, line 1, in ? NameError: set I'm curious why a tuple was chosen. Tuples are smaller and faster to build than lists -- they're the most lightweight sequence type in Python. You don't need all the extra functionality of lists, so why go to the time and effort of building a list? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: list vs. tuple [Re: len() should always return something]
Steven D'Aprano wrote: On Fri, 24 Jul 2009 15:03:29 -0400, Roy Smith wrote: In article mailman.3674.1248461573.8015.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: Better:if isinstance(x, (int, float, complex)): I never noticed this before, but it seems odd that the second argument to isinstance() should be a tuple. Using the normal arguments made about tuples vs. lists, it seems like a list would be the right data structure here. I ignore the 'normal arguments' and it seems that Guido or the designer of isinstance did so here too. Fortunately. Practicality beats 'purity', especially misguided purity. What would be the point of using a list? You're never going to sort it, or append items to it, or otherwise mutate it. You build it, pass it to a function which doesn't modify it in any fashion, then it gets garbage collected. I suppose a set would be even more right, but (I'm pretty sure) isinstance() predates sets. Yes. [st...@sylar ~]$ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam isinstance built-in function isinstance set Traceback (innermost last): File stdin, line 1, in ? NameError: set I'm curious why a tuple was chosen. Tuples are smaller and faster to build than lists -- they're the most lightweight sequence type in Python. You don't need all the extra functionality of lists, so why go to the time and effort of building a list? In fact, constant tuples can be and now are compiled as constants: dis(compile('(1,2,3)','','eval')) 1 0 LOAD_CONST 3 ((1, 2, 3)) 3 RETURN_VALUE dis(compile('[1,2,3]','','eval')) 1 0 LOAD_CONST 0 (1) 3 LOAD_CONST 1 (2) 6 LOAD_CONST 2 (3) 9 BUILD_LIST 3 12 RETURN_VALUE Internally, even a frozenset is more complicated than a tuple since it still needs a hash table, which is overkill for something that will be linearly scanned exactly once. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: list vs. tuple [Re: len() should always return something]
Roy Smith r...@panix.com wrote: In article mailman.3674.1248461573.8015.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: Better:if isinstance(x, (int, float, complex)): I never noticed this before, but it seems odd that the second argument to isinstance() should be a tuple. Using the normal arguments made about tuples vs. lists, it seems like a list would be the right data structure here. I suppose a set would be even more right, but (I'm pretty sure) isinstance() predates sets. I'm curious why a tuple was chosen. There's a very good reason[*]. The second argument to isinstance() is a classinfo where a classinfo is a type or a tuple of classinfo. That means you can have an arbitrarily complex set of nested tuples e.g. (int, (float, complex)), but the immutable nature of tuples means the implementation of isinstance can walk the tuple tree without ever having to worry about infinite loops. If classinfo could be a list of types then someone somewhere would feed it a recursive list and then complain that the interpreter crashed. Exception specifications are tuples for the same reason. [*] For some definition of 'very good'. Would Python be significantly impaired if you couldn't combine classinfos into a tree structure? Probably not. -- http://mail.python.org/mailman/listinfo/python-list
Re: list vs. tuple [Re: len() should always return something]
isinstance(x, (int, float, complex)) is certainly very compact, and does what I want. Thanks! -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654347.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list