Re: Bug or feature?

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Mon, 29 Aug 1994 10:29:52 -0400

On Aug 29, 11:27, John Wezel wrote:
>
> class test:
> def __init__ (self, arg):
> try:
> self.attr = 1 / arg
> print 'Success'
> except:
> print 'Exception'
>
> def __del__ (self):
> print 'Delete'
>
> A test for this might look like:
>

>>> a = test(1)
Success
>>> del a
Delete
>>> b = test(0)
Exception
>>> b
<test instance at 20067dc8>
>>> del b
>>> b
Traceback (innermost last):
File "<stdin>", line 1
NameError: b
>>> dir()
['_', '__name__', 'test']
>>>
>>> c = test( 0.0 )
Delete
Exception

Note that the delete is deferred.

After I tried this test, I read Guido's reply that explains why
there is still a lingering reference to self.

The next exception from 'c' clears the saved reference from 'b'
and b's __del__ method is finally called.

And a while back, in answer to another "why isn't '__del__' being
called" question, I posted a partial explaination of reference
counting and 'del' in Python: i.e. 'del' doesn't necessarily execute
__del__ - it decrements the objects reference count, and when the
reference count goes to zero, __del__ is called and then object is
actually deleted.

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics