Death to tuples!

Antoon Pardon apardon at forel.vub.ac.be
Mon Nov 28 08:23:11 EST 2005


Op 2005-11-28, Peter Hansen schreef <peter at engcorp.com>:
> 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)

I'm probably missing something, but what would be the problem if this
list was created during compile time?

-- 
Antoon Pardon



More information about the Python-list mailing list