remark 发表于 2014-08-14 19:25

【新手】这个错误是什么意思

class Person:
      population = 0

      def __init__(self, name):
                self.name = name
                print 'init %s' % self.name
                Person.population += 1

      def __del__(self):
                print 'destructor %s' % self.name
                Person.population -= 1

david = Person('david')

./obj.py
init david
destructor david
Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0x7faff27a6f80>> ignored

remark 发表于 2014-08-15 15:38

从书上抄的例子代码,执行出错,不过没想到这个问题居然这么难,没人会

Linux_manne 发表于 2014-08-15 17:00

Warning: Due to the precarious circumstances under which __del__() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead. Also, when __del__() is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the __del__() method may already have been deleted. For this reason, __del__() methods should do the absolute minimum needed to maintain external invariants. Starting with version 1.5, Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the __del__() method is called.

Linux_manne 发表于 2014-08-15 17:04

看also 后面那段话

remark 发表于 2014-08-16 13:45

多谢,看到了
那这是个系统问题了? 还以为和C++里的类成员静态变量有些类似
页: [1]
查看完整版本: 【新手】这个错误是什么意思