- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I correctly clean up a Python object?
Cleanup happens to globals by setting them to None. The locals self destruct at the end of the session. The function __del__ called by Python sets the globals to None.
Consider the following code where there is clean up of all objects in the given class −
class Counter: Count = 0 # This is the count of objects of this class def __init__(self, name): self.name = name print name, 'created' Counter.Count += 1 def __del__(self): print self.name, 'deleted' Counter.Count -= 1 if Counter.Count == 0: print 'Last Counter object deleted' else: print Counter.Count, 'Counter objects remaining' x = Counter("First") del x
Without the final del, you get an exception.
From the Python docs regarding __del__ −
Warning: Due to the precarious circumstances under which __del__() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead. Also, when __del__() is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the __del__() method may already have been deleted. For this reason, __del__() methods should do the absolute minimum needed to maintain external invariants.
Without the explicit call to del, __del__ is only called at the end of the program, Counter and/or Count may have already been GC-ed by the time __del__ is called (the order in which objects are collected is not deterministic). The exception means that Counter has already been collectd. You can’t do anything particularly fancy with __del__.
There are two possible solutions here.
Use an explicit finalizer method, such as close() for file objects.
Use weak references.
- Defining Clean Up Actions in Python
- How do I look inside a Python object?
- How to clean up your online digital footprint?
- How do I automatically download files from a pop up dialog using selenium-python?
- How do I create a java.sql.Date object in Java?
- How do you round up a float number in Python?
- How do I make a pop-up in Tkinter when a button is clicked?
- How do I create a Python namespace?
- How do I set up C/C++ on Eclipse in Windows?
- How do I prevent jQuery events from bubbling up to parent elements?
- How do I unload (reload) a Python module?
- How do I copy a file in python?
- How do I do a case insensitive string comparison in Python?
- How do I install Python SciPy?