我正在从this web page学习Python(它是中文,请专注于代码)并且我想在windows7上使用python2.7.3来练习代码。但是我发现对象名有一个非常奇怪的错误。代码如下:
class Person:
'''Represents a person.'''
population = 0
def __init__(self, name):
'''Initializes the person\'s data.'''
self.name = name
print '(Initializing %s)' % self.name
# When this person is created, he/she
# adds to the population
Person.population += 1
def __del__(self):
'''I am dying.'''
print '%s says bye.' % self.name
Person.population -= 1
if Person.population == 0:
print 'I am the last one.'
else:
print 'There are still %d people left.' % Person.population
def sayHi(self):
'''Greeting by the person.
Really, that\'s all it does.'''
print 'Hi, my name is %s.' % self.name
def howMany(self):
'''Prints the current population.'''
if Person.population == 1:
print 'I am the only person here.'
else:
print 'We have %d persons here.' % Person.population
David = Person('David')
David.sayHi()
David.howMany()
kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
David.sayHi()
David.howMany()
我在运行这些代码时得到了这个错误报告(最后两行)。但是当我把对象变量'David'替换为'Swaroop'或其他名称时,代码可以正常工作!我不知道这是怎么发生的。在
^{pr2}$
我能找到的唯一解释来自^{} documentation (在警告块中):
为了验证这一点,我在
__del__
方法中添加了print globals()
:第一个调用:
Person
出现在带有适当类对象的全局变量中第二个调用:
Person
仍然出现,但是现在有一个None
值现在,为什么改变变量名
David
会改变这种行为?破坏的顺序可能是确定的,但规则没有记录在案:我们处于“实现定义”的领域。在本例中,在我的特定Python解释器中,将
David
重命名为A
确实改变了销毁顺序,Person
类对象最后被销毁。在这本身并不是一个修复,它只是一个奇怪的副作用:您不能合理地依赖销毁命令。在
要显示未调用
__del__
的示例:您可能会得到这样的输出:
^{pr2}$您可以得到(至少在Python2.7中):
当解释器关闭期间调用
__del__()
方法时,会出现异常。它不能保证此时任何东西都是可用的,在您的例子中,Person
变量似乎已经被销毁了。您应该忽略此问题或停止使用__del__()
。在相关问题 更多 >
编程相关推荐