Optimization of Tuples and Strings

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Jan 7 04:48:51 EST 2002


"Greg Krohn" <infinitystwin.SPAM at IS.BAD.yahoo.com> wrote in
news:a1bmh6028fk at enews2.newsguy.com: 
> I'm not sure about the extent of the this, though. I think Python
> keeps ints up to a certain point (100?) around, because those numbers
> are common. But the string thing suprised me; I only remember reading
> about integers. 
> 
Be careful about what you assume here:
>>> x = '1'
>>> y = '1'
>>> id(x)
8144192
>>> id(y)
8144192
>>> y = 'a b'
>>> x = 'a b'
>>> id(x)
8065952
>>> id(y)
8284856
>>>

If you put this into a script:
x = '1'
y = '1'
print id(x), id(y)
x = 'a b'
y = 'a b'
print id(x), id(y)

then the output is something like:
D:\Python>test.py
8144368 8144368
8269168 8269168

The rule is something along these lines: Identical constant strings in a 
script or module are folded together. Constant strings in interactive mode 
are folded if they look vaguely like identifiers.

You can use id() to compare any strings (whether constant or not), if you 
use the intern() builtin on the strings before comparing (but this means 
the memory for that string will never be freed):
>>> x = 'x'+'y'
>>> y = 'x'+'y'
>>> id(x), id(y)
(8276968, 8278040)
>>> x = intern(x)
>>> y = intern(y)
>>> id(x), id(y)
(8276968, 8276968)

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list