Equality and identity

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Tue Mar 30 17:29:06 EST 2004


On Tue, 30 Mar 2004 22:12:41 +0200, Marius Bernklev wrote:
> cookedm+news at physics.mcmaster.ca (David M. Cooke) writes:
>
>> In your example, 5 and "hei", for various reasons, are compiled to
>> reference the exact same object, so 'is' returns True. However, no
>> such caching is done with tuples, so (5,2) and (5,2) compile to
>> references to different objects (whose contents are equal).
>
> Thanks!

Also note that this is implementation-dependent, and is not guaranteed
to remain the same for any given type.  If you code such that you are
depending on any particular identity of equal values, your code becomes
brittle and will break when a different optimisation scheme is used in
the Python implementation.

-- 
 \     "It's a small world, but I wouldn't want to have to paint it."  |
  `\                                                  -- Steven Wright |
_o__)                                                                  |
Ben Finney <http://bignose.squidly.org/>



More information about the Python-list mailing list