小编wei*_*hen的帖子

无法在任何类中调用析构函数

我想在一个类中使用ConfigParser,它需要来自析构函数的调用,但随后会发生奇怪的事情.

这是我的代码:

"""
function name is 
test  ok!
test1 failed!
test2 failed!
test3 failed!

test4
...  ok!

"""

def test3():
  pass

class Class1:
  def __del__(self):
    test3()

if __name__=="__main__":
  obj=Class1()
Run Code Online (Sandbox Code Playgroud)

如果函数被命名test1,test2或者test3,将引发异常,我无法捕获它try except.

追溯是:

E:\tc.py
Exception TypeError: "'NoneType' object is not callable" in <bound method Class1.__del__ of <__main__.Class1 instance at 0x00C18F58>> ignored
Run Code Online (Sandbox Code Playgroud)

太奇怪了!你能在你的系统中测试它吗?或者我做错了什么?

python

2
推荐指数
1
解决办法
1860
查看次数

标签 统计

python ×1