我正在阅读 Python Essential Reference 第 4 版。我无法弄清楚如何解决以下代码中的问题
class Account(object):
num_accounts = 0
def __init__(self, name, balance):
self.name = name
self.balance = balance
Account.num_accounts += 1
def __del__(self):
Account.num_accounts -= 1
def deposit(self, amt):
self.balance += amt
def withdraw(self, amt):
self.balance -= amt
def inquire(self):
return self.balance
class EvilAccount(Account):
def inquire(self):
if random.randint(0,4) == 1:
return self.balance * 1.1
else:
return self.balance
ea = EvilAccount('Joe',400)
__del__
如果我理解正确,当程序结束并且应该调用继承的函数时, ea 对象会超出范围,对吗?我收到一个'NoneType' object has no attribute num_accounts
in __del__
。为什么它不在__init__
函数中更早地抱怨?