Reputation: 467
I am faced with the problem: A function receives references to objects and calls del on those references at a certain point during its routine.
Those references refer to objects that are sometimes referenced elsewhere and that sometimes are not.
I would like the del statement to only delete the reference present in the function. If that object is not referenced to elsewhere in the program, it can be garbage collected. If i'm correct, del directly calls the destructor of object rather than simply delete its reference. How can i get the desired behavior?
Upvotes: 1
Views: 91
Reputation: 42007
If i'm correct, del directly calls the destructor of object rather than simply delete its reference.
Assuming CPython, this is incorrect.
del
decreases the reference count of the object by 1. And the object is garbage collected when the reference count reaches 0.
If by destructor you meant the __del__
magic method, note that it is usually called when the object is being garbage collected, not when del
is called. Moreover, there's no guarantee that the method would even be called.
Just to note, there is also an (optional) gc
interface that can break object reference cycles.
Upvotes: 2