__init__ vs. __del__

Paul McGuire ptmcg at austin.rr.com
Sat Mar 21 21:44:50 EDT 2009


On Mar 21, 8:37 pm, Christian Heimes <li... at cheimes.de> wrote:
> Randy Turner wrote:
> > I was reading a book on Python-3 programming recently and the book stated that, while there is an __init__ method for initializing objects, there was a __del__ method but the __del__ method is not guaranteed to be called when an object is destroyed.
>
> > If there is code in the __init__ method that allocates resources (memory, file opens, etc.), how do these resources get cleaned up when an object is destroyed?  Custom method?
>
> > At the moment, this architecture seems a bit asymmetric if the __del__ method is not called.
>

If you have resources in Python that need reliable open/close, lock/
unlock, etc. symmetry, use the with statement or try-finally in your
Python code.

-- Paul




More information about the Python-list mailing list