To clarify how Python handles two equal objects

Richard Damon Richard at Damon-Family.org
Wed Jan 11 13:55:48 EST 2023


I think the key point is that "the operation" doesn't act on "names" but 
on "objects" (which are different sort of things), and thus there isn't 
an "the other" when talking about the object being operated on.

Thinking of an operation being on a "name" is the mental model error. 
The only operations that operate on "names" are assignment operations.

Augmented assignment operations are more complicatd, as they can either 
work on the object the name points on, if that object is mutable, or 
rebinds the name to a new object if it isn't.

Thus a += b is NOT neccessarilily the same as a = a + b, as a += b might 
just mutate the object that a is bound to or might rebind in the manner 
of a = a + b;

Thus:

a = [ 1, 2, 3]
b = a
a += [4]

will change the single list that a and b are bound to into [1, 2, 3, 4], 
while

a = "foo"
b = a
a += "bar"

will change a to be bound to the string object "foobar" but not b, since 
the string object "foo" wasn't mutable.

Brings up the point, that you need to be careful with augmented 
assignment operators.

On 1/11/23 1:28 PM, Jen Kris via Python-list wrote:
> Thanks for your comments.  After all, I asked for clarity so it’s not pedantic to be precise, and you’re helping to clarify.
>
> Going back to my original post,
>
> mx1 = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
> arr1 = mx1[2]
>
> Now if I write "arr1[1] += 5" then both arr1 and mx1[2][1] will be changed because while they are different names, they are the assigned same memory location (pointer).  Similarly, if I write "mx1[2][1] += 5" then again both names will be updated.
>
> That’s what I meant by "an operation on one is an operation on the other."  To be more precise, an operation on one name will be reflected in the other name.  The difference is in the names,  not the pointers.  Each name has the same pointer in my example, but operations can be done in Python using either name.
>
>
>
>
> Jan 11, 2023, 09:13 by roel at roelschroeven.net:
>
>> Op 11/01/2023 om 16:33 schreef Jen Kris via Python-list:
>>
>>> Yes, I did understand that.  In your example, "a" and "b" are the same pointer, so an operation on one is an operation on the other (because they’re the same memory block).
>>>
>> Sorry if you feel I'm being overly pedantic, but your explanation "an operation on one is an operation on the other (because they’re the same memory block)" still feels a bit misguided. "One" and "other" still make it sound like there are two objects, and "an operation on one" and "an operation on the other" make it sound like there are two operations.
>> Sometimes it doesn't matter if we're a bit sloppy for sake of simplicity or convenience, sometimes we really need to be precise. I think this is a case where we need to be precise.
>>
>> So, to be precise: there is only one object, with possible multiple names to it. We can change the object, using one of the names. That is one and only one operation on one and only one object. Since the different names refer to the same object, that change will of course be visible through all of them.
>> Note that 'name' in that sentence doesn't just refer to variables (mx1, arr1, ...) but also things like indexed lists (mx1[0], mx1[[0][0], ...), loop variables, function arguments.
>>
>> The correct mental model is important here, and I do think you're on track or very close to it, but the way you phrase things does give me that nagging feeling that you still might be just a bit off.
>>
>> -- 
>> "Peace cannot be kept by force. It can only be achieved through understanding."
>>   -- Albert Einstein
>>
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list
>>

-- 
Richard Damon



More information about the Python-list mailing list