Movatterモバイル変換
[0]ホーム
Beginner's Question (or bug in python?)
Rolf Magnusramagnus at zvw.de
Fri Apr 20 10:47:52 EDT 2001
Hello. I have a problem understanding why the behaviour of python depends on class names. I have the following python script:#!/usr/bin/pythonclass Fred: def __init__(self): print('initializing fred') def __del__(self): print('deleting fred') def test(self): print('Hello world')class Foo(Fred): def __init__(self): Fred.__init__(self) print ('initializing foo') def __del__(self): print('deleting foo'); Fred.__del__(self)x = Foo()x.test()If I execute this, I get the following:initializing fredinitializing fooHello worlddeleting fooException exceptions.AttributeError: "'None' object has no attribute '__del__'" in <method Foo.__del__ of Foo instance at 80c3c00> ignoredIf I instead call the base class Hans instead of Fred, it works as expected:initializing fredinitializing fooHello worlddeleting foodeleting fredThis seems to be the case for Python 1.52 and 2.1 (not sure about 2.1, because I only tested the first case and got the error message).And if I add a "del x" after the last line, it works in both cases. This seems odd to me, but since I am a python beginner, there is perhaps something important I don't know.
More information about the Python-listmailing list
[8]ページ先頭