Destructor

Gustavo Cordova gcordova at hebmex.com
Wed May 22 09:48:23 EDT 2002


> 
> Rajat Chopra <rajat at cs.utexas.edu> writes:
> 
> > I have a very quick and basic question -- but I was just 
> > when exactly a Destructor is invoked? Do you have to delete
> > the object (ex: del OBJECT ) or is it enough to say
> > OBJECT = None ???
> 

This is a visual basic idiom, setting an object to Nothing in order
to destruct a referenced object. No need for that in Python.

If you have an object reference in a variable, setting that variable
to another value frees the previous reference; and if that object
does not have any other strong references (it can have weak references)
then it may be destructed.

"May" be destructed, because there's no guaranteed moment the
destructor can be called. I CPython's case, the destructor is called
once it's detected that there are no more references to an object,
but in other implementations it may behave differently.

So, no need to do "obj = None" to free an object, just "del obj" is
enough, if you absolutely can't wait for the variable to loose scope
and dissapear on it's own.

-gustavo





More information about the Python-list mailing list