I am very confused with the python code below:
>>> class A(): pass
...
>>> id(A()) == id(A())
True
>>> id(A())
19873304
>>> id(A())
19873304
>>> A() is A()
False
>>> a = A()
>>> b = A()
>>> id (a) == id (b)
False
>>> a is b
False
>>> id (a)
19873304
>>> id (b)
20333272
>>> def f():
... print id(A())
... print id(A())
...
>>> f()
20333312
20333312
I can tell myself clearly what python doing when creating objects.
Can anyone tell me more about what happend? Thanks!
An acid is any hydrogen-containing substance that is capable of donating a proton (hydrogen ion) to another substance. A base is a molecule or ion able to accept a hydrogen ion from an acid.
The chemical formula begins with H in case of acids. For example, Hydrochloric acid (HCl). The Chemical formula ends with OH in case of bases. For example, Sodium Hydroxide (NaOH).
(i) Acids are sour to taste. Bases are bitter to taste. (ii) Acids turn blue litmus red. Bases do not change the colour of blue litmus.
A Brønsted-Lowry acid is any species that can donate a proton (H+) to another molecule. A Brønsted-Lowry base is any species that can accept a proton from another molecule. In short, a Brønsted-Lowry acid is a proton donor (PD), while a Brønsted-Lowry base is a proton acceptor (PA).
When you say
print id(A()) == id(A())
you are creating an object of type A
and passing it to id
function. When the function returns there are no references to that object created for the parameter. So, the reference count becomes zero and it becomes ready for garbage collection.
When you do id(A())
in the same expression again, you are trying to create another object of the same type. Now, Python might try to reuse the same memory location which was used for the previous object created (if it is already garbage collected). Otherwise it will create it in a different location. So, id
may or may not be the same.
If you take,
print A() is A()
We create an object of type A
and we are trying to compare it against another object of type A
. Now the first object is still referenced in this expression. So, it will not marked for garbage collection and so the references will be different always.
Suggestion: Never do anything like this in production code.
Quoting from the docs,
Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the is operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info.
Two different objects can be at the same location in memory, if one of them is freed before the other is created.
That is to say -- if you allocate an object, take its id, and then have no more reference to it, it can then be freed, so another object can get the same id.
By contrast, if you retain a reference to the first object, any subsequent object allocated will necessarily have a different id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With