Question:
In researching, I noticed that the id()
function returns an integer and that it is guaranteed to be unique and constant for the object.
When comparing two objects with different results, what could have made these different results possible??
I noticed in a handout that the comparison id(Carro()) == id(Carro())
returns False
but when executing the code it returned True
Car.py class
class Carro:
pass
Code no Idle
>>> from Carro import Carro
>>> fusca = Carro()
>>> opala = Carro()
>>> id(opala) == id(fusca)
False
>>> id(Carro()) == id(Carro())
True
Answer:
In fact you didn't create two instances in:
>>> id(Carro()) == id(Carro()) # True
that's why True:
See the code below, it works on python2 or python3
>>> from carro import Carro
>>> fusca = Carro()
>>> opala = Carro()
>>> id(opala) == id(fusca)
False
>>> id(Carro()) == id(Carro())
True
>>> a = id(Carro())
>>> b = id(Carro())
>>> a
140356195163608
>>> b
140356195163720
>>> id(Carro())
140356195163608