Ad hoc lists vs ad hoc tuples

Duncan Booth duncan.booth at invalid.invalid
Thu Jan 28 04:45:21 EST 2010


Terry Reedy <tjreedy at udel.edu> wrote:

> Constant tuples (a tuple whose members are all seen as constants by the 
> compiler) are now pre-compiled and constructed once and put into the 
> code object as such rather than re-constructed with each run of the code.

Sadly that's not entirely accurate.

Tuples whose members are all simple constants are pre-compiled, but here's 
an example of a tuple whose members are all constants but doesn't get 
optimised:

>>> def foo():
	data = (
		('hello', 42),
		('world', 24),
		)
	return data

>>> import dis
>>> dis.dis(foo)
  3           0 LOAD_CONST               5 (('hello', 42)) 

  4           3 LOAD_CONST               6 (('world', 24)) 
              6 BUILD_TUPLE              2 
              9 STORE_FAST               0 (data) 

  6          12 LOAD_FAST                0 (data) 
             15 RETURN_VALUE     


-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list