I decided to write a memorandum to take this opportunity to summarize what I ended up with just an image.
This article is based on Fluent Python-Pythonic Thinking and Coding Techniques.
First of all, from the dictionary meaning
** 1 Different things are indistinguishable by their nature. ** ** ** 2 Things are the same to themselves over time and place. Self-identity. Independence. identity. ** **
This time, I used goo dictionary.
Suppose a person whose real name is Lisa is working under the name Emma. In this case, not only the names are the same, but the contents are also the same. Furthermore, suppose a person named Lily spoofed as Lisa. If this case is represented programmatically
Lisa = {'name': 'Lisa', 'born': 1996, 'job': 'singer'}
Emma = Lisa;
Lily = {'name': 'Lisa', 'born': 1996, 'job': 'singer'}
print(id(Lisa));
print(id(Emma));
print(id(Lily));
print((Lisa) is (Emma));
print((Lisa) is (Lily));
When this is displayed
4540344832
4540344832
4540344912
True
False
It looks like this. You can see that Lisa and Emma have the same ID, but Lily has a different ID. Even if you use the is operator, you can see that Lisa and Emma display False and are different.
Also from a dictionary meaning ** 1 The value and price are the same. Same price. ** ** ** 2 Equivalent (how) ** So when I looked up the equivalence
** In logic, in two propositions p and q, when one is true, the other is true, and if one is false, the other is false, then p and q are equivalent. Also, when "q if p" and "p if q" hold at the same time, p and q are said to be equivalent. Equal value. Equivalent. Equivalent. ** ** It was said. Then, as before, the program
Lisa = {'name': 'Lisa', 'born': 1996, 'job': 'singer'}
Emma = Lisa;
Lily = {'name': 'Lisa', 'born': 1996, 'job': 'singer'}
print(id(Lisa));
print(id(Emma));
print(id(Lily));
print((Lisa) is (Emma));
print((Lisa) is (Lily));
print((Lisa) == (Emma));
I just added print ((Lisa) == (Emma));
to the last line ... lol
When this is displayed
4431907408
4431907408
4431907488
True
False
True
It looks like this. Well, the ID is different, but the contents are the same, so if you use the == operator, it will be True.
From a dictionary meaning ** Alias is an English word that has meanings such as pseudonyms, aliases, and common names. In the IT field, it refers to a mechanism that allows an object or entity to be referred to in the same way by multiple different symbols or identifiers. alias. ** ** Well, it's like a nickname. Emma is also an alias for Lisa.
The two variables Lisa and Emma are aliases that are bound to the same object and have the same identity. The objects bound to Lisa and Lily have the same value but are not the same. There is equivalence. ~~ It may not be organized lol ~~
You pointed out that aliases are like passing by reference. But python can't. Please refer to it when passing by reference in python. Programming FAQ
Recommended Posts