on-internet
← Community

Curious about order of prints when deleting object references in Python

Stack Overflow ·

I create an object using a class. (The class has a del method.) I then create an alias to the object variable. I then delete the 2 variable references to the object. After this has happened, python automatically calls __del_. But in the python shell, prints from within __del_ appear before other top-level prints. Is this because, internally, __del_ is run as a higher priority thread than top-level prints? class MyClass: def __del__(self): print("Object finalized") obj1 = MyClass() obj2 = obj1 del obj1 print("First reference removed") del obj2 print("Second reference removed") Shell output: Fir

Comments

Sign in or sign up to comment and upvote.

No comments yet.