Newbie look at Python and OO
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Thu May 10 17:55:28 EDT 2007
walterbyrd a écrit :
> Thanx for all the replies, I may be slowly getting it. But, can
> anybody explain this?
>
>
>>>>a = 'hello'
>>>>b = 'hello'
>>>>a is b
>
> True
>
>>>>a = 'hello there'
>>>>b = 'hello there'
>>>>a is b
>
> False
>
Python - well, CPython (the reference C implementation) at least - tries
to optimize memory usage by "interning" (caching/reusing) string objects
when possible. You'll find a similar behaviour with integers:
>>> a = 5
>>> b = 5
>>> a is b
True
>>> a = 50000
>>> b = 50000
>>> a is b
False
>>>
IOW, this is an implementation detail. FWIW, you should not use identity
tests on immutable objects (the None object set aside) - since they are
immutable, equality test is enough.
More information about the Python-list
mailing list