Python Forum

Full Version: I cannot understand aliases, apparently
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I guess the quickest way is to show you my interaction:
>>> a = 3
>>> b = a
>>> a is b
True
>>> a = 4
>>> print (b)
3

a) What I thought: by assigning b = a, I've created an alias for a. Basically, b is another reference to the same object that a refers to.
b) I confirmed that (?) by asking if 'a is b' and received True, meaning they are both the same object
c) Then I refute my own conjecture by changing a and querying b, seeing that they are not the same object, as the object referred to by 'a' has been modified, but the object referred to by 'b' remains unmodified...

Now I am confused.

Sorry for newbie question, I really tried googling this first!

My current explanation is that I am being confused with another programming language, where everything is an object, as opposed to python, where integers are not objects and therefore a = b does not create an alias.

(for example: with lists instead of integers, the interaction with python console shows aliasing working like the book says)

However, in that case, why did 'a is b' return True?
You have reassigned what a points to, so a no longer is b. Here's a longer interchange that might make it more clear.
>>> a=3
>>> b=a
>>> a is b
True
>>> c = 4
>>> a = c
>>> c is a
True
>>> a is b
False
>>> 
When a is reassigned to c it is no longer linked to b.
Reminds me of quantum entanglement ;^).
(Aug-29-2019, 12:09 AM)Mustey Wrote: [ -> ]c) Then I refute my own conjecture by changing a and querying b, seeing that they are not the same object, as the object referred to by 'a' has been modified, but the object referred to by 'b' remains unmodified...

That´s the error in your thinking. The object referred to by a hasn´t been modified in any way.
By assigning a = 4 you created a new object 4 and the name a is pointing to that object.
The object 3 is still there and b is pointing to it.
(Aug-29-2019, 01:06 AM)jefsummers Wrote: [ -> ]You have reassigned what a points to, so a no longer is b. Here's a longer interchange that might make it more clear.
>>> a=3
>>> b=a
>>> a is b
True
>>> c = 4
>>> a = c
>>> c is a
True
>>> a is b
False
>>> 
When a is reassigned to c it is no longer linked to b.
Reminds me of quantum entanglement ;^).

In Python, all the variables are holding the reference of memory object. When a is 3 and a=b mean, a and b are pointing to same memory location where we stored value 3 and when a is assigned to new value (say 4), it changes its memory reference from value 3 to 4. However memory reference of value 3 is still valid for variable b and assigning another reference to variable 'a' won't make any change in variable 'b' reference
Take a look at https://nedbatchelder.com/text/names.html
and, yes, everything is object in python, including int.
One thing to keep in mind that Python does some background optimisation and object identity can be different for immutable objects based on value:

>>> a = 256
>>> b = 256
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False
Everyone should watch this video: https://www.youtube.com/watch?v=_AEJHKGk9ns
I guess there are some points explained, you didn't know before.
(Aug-29-2019, 07:51 AM)DeaD_EyE Wrote: [ -> ]Everyone should watch this video: https://www.youtube.com/watch?v=_AEJHKGk9ns

for those that prefer reading vs youtube - link to same presentation as blog in my post above