Python Forum
I cannot understand aliases, apparently
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I cannot understand aliases, apparently
#1
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?
Reply
#2
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 ;^).
Reply
#3
(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.
Reply
#4
(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
Reply
#5
Take a look at https://nedbatchelder.com/text/names.html
and, yes, everything is object in python, including int.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
Everyone should watch this video: https://www.youtube.com/watch?v=_AEJHKGk9ns
I guess there are some points explained, you didn't know before.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mypy, type aliases and type variables tomciodev 1 692 Oct-18-2023, 10:08 AM
Last Post: Larz60+
  Pip apparently installed but unrecognized by CMD diegoctn 13 9,306 Apr-05-2018, 02:55 PM
Last Post: snippsat
  os.path.exists apparently doesn't always work! Larz60+ 2 4,858 Oct-10-2017, 10:16 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020