Python Frequently Asked Questions, Release 3.9.5 to reproduce a problem. Worse, the order in which object’s$_{__del__()}$ methods are executed is arbitrary. You can run gc.collect() to force a collection, but there are pathological cases where objects will never be collected. Despite the cycle collector, it’s still a good idea to define an explicit$_{close()}$ method on objects to be called whenever you’re done with them. The close() method can then remove attributes that refer to subobjects. Don’t call$_{__del__()}$ directly –$_{__del__()}$ should call$_{close()}$ and$_{close()}$ should make sure that it can be called more than once for the same object. Another way to avoid cyclical references is to use the weakref module, which allows you to point to objects without incrementing their reference count. Tree data structures, for instance, should use weak references for their parent and sibling references (if they need them!). Finally, if your$_{__del__()}$ method raises an exception, a warning message is printed to$_{sys.stderr}$ . Python does not keep track of all instances of a class (or of a built-in type). You can program the class’s constructor to keep track of all instances by keeping a list of weak references to each instance. The$_{id()}$ builtin returns an integer that is guaranteed to be unique during the lifetime of the object. Since in CPython, this is the object’s memory address, it happens frequently that after an object is deleted from memory, the next freshly created object is allocated at the same position in memory. This is illustrated by this example: The two ids belong to different integer objects that are created before, and deleted immediately after execution of the id() call. To be sure that objects whose id you want to examine are still alive, create another reference to the object: The$_{is}$ operator tests for object identity. The test$_{a is b}$ is equivalent to$_{id(a) == id(b)}$ . The most important property of an identity test is that an object is always identical to itself,$_{a is a}$ always returns $_{True}$. Identity tests are usually faster than equality tests. And unlike equality tests, identity tests are guaranteed to return a boolean$_{True}$ or$_{False}$. However, identity tests can only be substituted for equality tests when object identity is assured. Generally, there are three circumstances where identity is guaranteed: In most other circumstances, identity tests are inadvisable and equality tests are preferred. In particular, identity tests should not be used to check constants such as$_{int}$ and$_{str}$ which aren’t guaranteed to be singletons: 1) Assignments create new names but do not change object identity. After the assignment$_{new = old}$ , it is guar- anteed that$_{new is old}$. 2) Putting an object in a container that stores object references does not change object identity. After the list assign- ment$_{s[0] = x}$ , it is guaranteed that$_{s[0] is x}$. 3) If an object is a singleton, it means that only one instance of that object can exist. After the assignments$_{a =}$ None and$_{b = None}$, it is guaranteed that$_{a is b}$ because$_{None}$ is a singleton. 31 2.6. Objects 2.6.14 When can I rely on identity tests with the is operator? >>> a =1000 ; b=2000 >>> id(a) 13901272 >>> id(b) 13891296 >>> id(1000 ) 13901272 >>> id(2000 ) 13901272 2.6.13 Why does the result of id() appear to be not unique? 2.6.12 How do I get a list of all instances of a given class?