Death to tuples!

Peter Hansen peter at engcorp.com
Mon Nov 28 08:10:10 EST 2005


Mike Meyer wrote:
> It seems that the distinction between tuples and lists has slowly been
> fading away. What we call "tuple unpacking" works fine with lists on
> either side of the assignment, and iterators on the values side. IIRC,
> "apply" used to require that the second argument be a tuple; it now
> accepts sequences, and has been depreciated in favor of *args, which
> accepts not only sequences but iterators.
> 
> Is there any place in the language that still requires tuples instead
> of sequences, except for use as dictionary keys?

Would it be possible to optimize your "frozenlist" so that the objects 
would be created during compilation time and rather than only during 
runtime?  If not then tuples() have a distinct performance advantage in 
code like the following where they are used as local constants:

 >>> def func(x):
...   if x in (1, 3, 5, 7, 8):
...     print 'x is really odd'
...
 >>> import dis
 >>> dis.dis(func)
....
   3          20 LOAD_FAST                0 (x)
              23 LOAD_CONST               8 ((1, 3, 5, 7, 8))
              26 COMPARE_OP               6 (in)

 >>> def func(x):
...   if x in [1,3,5,7,8]:
...      print 'x is really odd'
...
 >>> dis.dis(func)
...
   3          20 LOAD_FAST                0 (x)
              23 LOAD_CONST               2 (1)
              26 LOAD_CONST               3 (3)
              29 LOAD_CONST               4 (5)
              32 LOAD_CONST               5 (7)
              35 LOAD_CONST               6 (8)
              38 BUILD_LIST               5
              41 COMPARE_OP               6 (in)


-Peter




More information about the Python-list mailing list