[Tutor] Objects, object references, object values and memory addresses
d at davea.name
Thu Oct 18 07:16:18 CEST 2012
On 10/17/2012 11:41 PM, boB Stepp wrote:
> >From Programming in Python 3, 2nd edition (p. 22-23):
>>>> a = ["Retention", 3, None]
>>>> b = ["Retention", 3, None]
>>>> a is b
>>>> b = a
>>>> a is b
> My current understanding is as follows: On the first two lines, two
> separate objects are defined, stored in two separate blocks of memory.
> These two objects just happen to have the same value, ["Retention", 3,
> None], stored in two separate locations. a and b, the object
> references (Variables are what I used to call these.), store these two
> separate memory locations. Thus a is b is false. However, when the
> line b = a is implemented, b now references the same object (memory
> location) as a, which now causes a is b to be true. Is my
> understanding correct?
You are correct, subject to an amendment. Using the term memory
addresses implies a particular implementation. CPython happens to work
that way, in its current implementation. Jython happens not to. No
biggie. a and b are simply bound to two different objects, and it's
that difference that causes a false result. When they're bound to the
same object, you get a true result.
> On the next page the author states (after giving a string example
> where a and b each are assigned the string "many paths", similar to
> the example above):
> "In some cases, comparing the identity of two strings or numbers--for
> example, using a is b--will return True, even if each has been
> assigned separately as we did here. This is because some
> implementations of Python will reuse the same object (since the value
> is the same and is immutable) for the sake of efficiency..."
> I ask: Which implementations of Python do this? In trying to make any
> code I write portable across as many platforms as possible, should I
> avoid using the identity operator, is (and its opposite, is not),
> except when I wish to compare to None?
Not just comparing to None, but to any singleton object. if you're sure
that you have only one instance of a particular object, for whatever
reason, then it's safe to use 'is' to distinguish this object from any
Which implementations do this? Any of them, starting with CPython,
which is probably the one you're using. Note that mutable objects that
are different must be distinct, so each time you create one you'll get a
unique object. It's only for immutable objects that the implementation
may decide to reuse existing objects. This includes, ints, floats,
strings, byte strings, tuples, etc. In the particular case of CPython,
small integers are cached in this way, and so are short strings with no
whitespace. How small, and exactly which strings is irrelevant to me,
and hopefully to you. The point is you cannot be sure whether equal
immutable objects are really just a single one, or not.
If you only care about value, then definitely use == or its variants.
If you are comparing against a singleton, then go ahead and use 'is'.
Otherwise, beware, and expect the unexpected.
More information about the Tutor