super not working in __del__ ?

Duncan Booth duncan.booth at invalid.invalid
Wed Feb 16 08:28:08 EST 2005


Ola Natvig wrote:
> Duncan Booth wrote:
>>>>>class Base(object):
>> 
>>      def __del__(self):
> 
> There should be a super(self.__class__, self)._del__() here if I'm not
> totaly wong, which could be the case here ;)
> 
> 
>>           print "Base.__del__"
>> 
>>           
> 

Thanks to Brian Beck for pointing out I hadn't read your question 
correctly.

There must not be a super call from the class Base. This is a common 
problem when using super: if the method you are propagating isn't defined 
by 'object' (and most aren't), then you must provide some way to terminate 
the chain of calls. One way to do this is to ensure that you have some base 
class which does not attempt to pass the call upwards. Then all you have to 
do is ensure that everything that has the method is a subclass of Base and 
this will ensure that the Base method will be the last method called in the 
super chain.

If I had included a super call:

>>> class Base(object):
	def __del__(self):
		print "Base.__del__"
		super(self.__class__, self).__del__()

		
>>> x = Base()
>>> del x
Base.__del__
Exception exceptions.AttributeError: "'super' object has no attribute 
'__del__'" in <bound method Base.__del__ of <__main__.Base object at 
0x00B43D90>> ignored
>>> 




More information about the Python-list mailing list