Movatterモバイル変換
[0]ホーム
[Python-Dev] Why does IOBase.__del__ call .close?
Nikolaus RathNikolaus at rath.org
Wed Jun 11 03:30:49 CEST 2014
Hello,I recently noticed (after some rather protacted debugging) that theio.IOBase class comes with a destructor that calls self.close():[0]nikratio at vostro:~/tmp$ cat test.pyimport ioclass Foo(io.IOBase): def close(self): print('close called')r = Foo()del r[0]nikratio at vostro:~/tmp$ python3 test.pyclose calledTo me, this came as quite a surprise, and the best "documentation" ofthis feature seems to be the following note (from the io libraryreference):"The abstract base classes also provide default implementations of some methods in order to help implementation of concrete stream classes. For example, BufferedIOBase provides unoptimized implementations of readinto() and readline()."For me, having __del__ call close() does not qualify as a reasonabledefault implementation unless close() is required to be idempotent(which one could deduce from the documentation if one tries to, but it'sfar from clear).Is this behavior an accident, or was that a deliberate decision?Best,-Nikolaus-- GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599FFingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F »Time flies like an arrow, fruit flies like a Banana.«
More information about the Python-Devmailing list
[8]ページ先頭