is this a bug?
Skip Montanaro
skip at pobox.com
Thu Aug 16 00:30:10 EDT 2001
David> Hmmm, I am sure there is a simple explanation. From the quickee
David> variation of you example below, I infer that the Python compiler
David> uses a dictionary for storing literals, so that only 1 copy is of
David> each unique literal is kept at compile time.
Give that man a cigar, but smoke half of it first... ;-) Right idea, but a
couple details are off.
Yes, the string (and int and float) literals as well as None are stored in a
tuple of constants in the code object created for each function (None is
always f.func_code.co_consts[0], even when it's not mentioned in the Python
code):
>>> def f():
... a="+="
... print a is a
... print a is "+="
...
>>> f()
1
1
>>> f.func_code.co_consts
(None, '+=')
Not stored by the current compiler are tuples or complex numbers of the form
N+Mj where N and M are ints or floats, but have to be built up using
multiple bytecode instructions:
>>> def g():
... a = (1,2,3)
... c = 1.0+8j
...
>>> g.func_code.co_consts
(None, 1, 2, 3, 1.0, 8j)
>>> import dis
>>> dis.dis(g)
0 SET_LINENO 1
3 SET_LINENO 2
6 LOAD_CONST 1 (1) <----
9 LOAD_CONST 2 (2) <----
12 LOAD_CONST 3 (3) <----
15 BUILD_TUPLE 3 <----
18 STORE_FAST 1 (a)
21 SET_LINENO 3
24 LOAD_CONST 4 (1.0) <----
27 LOAD_CONST 5 (8j) <----
30 BINARY_ADD <----
31 STORE_FAST 0 (c)
34 LOAD_CONST 0 (None)
37 RETURN_VALUE
don't-you-just-love-introspection?-ly, y'rs,
--
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/
More information about the Python-list
mailing list