
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 −
Example
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.
- Related Articles
- 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?
- My Python program is too slow. How do I speed it up?
- How do I create a java.sql.Date object in Java?
- How do I remove a property from a JavaScript object?
- How do you round up a float number in Python?
- How do I create a Python namespace?
- How do I make a pop-up in Tkinter when a button is clicked?
- How do I set up C/C++ on Eclipse in Windows?
- How do I unload (reload) a Python module?
- How do I copy a file in python?
- How do I delete a file in Python?
- How do I prepare for a Python interview?
