[PYTHON] Differences in identity, equivalence, and aliases

I decided to write a memorandum to take this opportunity to summarize what I ended up with just an image.

Introduction

This article is based on Fluent Python-Pythonic Thinking and Coding Techniques.

identity

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.

Equivalence

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.

alias

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.

Summary

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 ~~

Postscript

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

Differences in identity, equivalence, and aliases
Identity and equivalence Python is and ==
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in syntax between Python and Java
Differences in multithreading between Python and Jython
Differences between queryStringParameters and multiValueQueryStringParameters in AWS Lambda
Identity matrix and inverse matrix: Linear algebra in Python <4>
Calculate Pose and Transform differences in Python with ROS
Differences in prices by prefecture (2019)
Clipping and normalization in TensorFlow
Equivalence of objects in Python
Stack and Queue in Python
Unittest and CI in Python