Optimization of Tuples and Strings
Greg Krohn
infinitystwin.SPAM at IS.BAD.yahoo.com
Mon Jan 7 03:38:21 EST 2002
"Matt Gerrans" <mgerrans at mindspring.com> wrote in message
news:a1bik0$9b$1 at slb4.atl.mindspring.net...
> Anyone here know whether Python treats strings and tuples the way Java
does
> Strings? That is, since they are immutable, does it just "point"
multiple
> references to an identical tuple to the same object? For example:
>
> x = (1,2)
> y = (1,2)
>
> Are x and y really referring to the same object?
>
> - Matt
Sort of:
>>> x = (1, 2)
>>> y = (1, 2)
>>> id(x)
15639388
>>> id(y)
15620460
>>> x = 2
>>> y = 2
>>> id(x)
3114888
>>> id(y)
3114888
>>> x = '1'
>>> y = '1'
>>> id(x)
15370896
>>> id(y)
15370896
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.
More information about the Python-list
mailing list