[PYTHON] FAQ: Why is the comparison of numbers inconsistent?

>>> x = 100
>>> y = 100
>>> x == y
True
>>> x is y
True
>>> x = 1000
>>> y = 1000
>>> x == y
True
>>> x is y
False

is is not equivalent but the same comparison

The is operator compares whether the left and right are references to the same object. See the following example.

>>> x = []
>>> y = []
>>> x is y
False
>>> z = x
>>> x is z
True
>>> x.append(42)
>>> y
[]
>>> z
[42]

x and y are separate list objects created separately. So x is y will be False, and changing x will not affect y.

On the other hand, z refers to the same object as x. So x is z will be True, and if you change x (the list object referenced by), then z will of course change as well.

Integer type is immutable

An object whose value that the object indicates is determined when the object is created and does not change thereafter is called an immutable. The integer is immutable. See the sample code below.

>>> x = 42
>>> y = x
>>> x is y
True
>>> x += 1
>>> x
43
>>> y
42
>>> x is y
False

Immediately after y = x, x is y was True because x and y point to the same object representing 42. x + = 1 assigns another object representing 43 to x. So x is y becomes False.

Flyweight pattern

The Flyweight pattern is used for optimization when immutable objects such as integer types are used in large numbers. Instead of creating an object with that value every time a new value is needed, by sharing an existing object

There are merits such as.

(As of 09/07/2016) In CPython, integers from -5 to 256 are managed by the flyweight pattern, and the same object is always used. Therefore, if the value is in this range, x is y may be True even for objects that should have been created separately.

Recommended Posts

FAQ: Why is the comparison of numbers inconsistent?
Why is the first argument of [Python] Class self?
Reasons why hybrid encryption is used (comparison of encryption / decryption speeds)
Is there a secret to the frequency of pi numbers?
Is the lottery profitable? ~ LOTO7 and the law of large numbers ~
Is the probability of precipitation correct?
Science "Is Saito the representative of Saito?"
If the accuracy of the PCR test is poor, why not repeat the test?
What is the cause of the following error?
[python] [meta] Is the type of python a type?
Why is cross entropy used for the objective function of the classification problem?
Tips: Comparison of the size of three values
The update of conda is not finished.
The backslash of the Japanese keyboard is "ro"
The answer of "1/2" is different between python2 and 3
The origin of Manjaro Linux is "Mount Kilimanjaro"
The value of pyTorch torch.var () is not distributed
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
This is the only basic review of Python ~ 3 ~
Scraping the winning data of Numbers using Docker
I thought about why Python self is necessary with the feeling of a Python interpreter
Around the place where the value of Errbot is stored
What is the true identity of Python's sort method "sort"? ??
Zip 4 Gbyte problem is a story of the past
What is a recommend engine? Summary of the types
When you think the update of ManjaroLinux is strange
The copy method of pandas.DataFrame is deep copy by default