Mã này ...Có thể một sự khác biệt đơn giản trong các tên biến Python3 thay đổi cách mã chạy?
class Person:
num_of_people = 0
def __init__(self, name):
self.name = name
Person.num_of_people += 1
def __del__(self):
Person.num_of_people -= 1
def __str__(self):
return 'Hello, my name is ' + self.name
cb = Person('Corey')
kb = Person('Katie')
v = Person('Val')
Tạo các lỗi sau ...
Exception AttributeError: "'NoneType' object has no attribute 'num_of_people'" in <bound method Person.__del__ of <__main__.Person object at 0x7f5593632590>> ignored
Nhưng mã này thì không.
class Person:
num_of_people = 0
def __init__(self, name):
self.name = name
Person.num_of_people += 1
def __del__(self):
Person.num_of_people -= 1
def __str__(self):
return 'Hello, my name is ' + self.name
cb = Person('Corey')
kb = Person('Katie')
vb = Person('Val')
Sự khác biệt duy nhất tôi thấy là tên biến cuối cùng là "vb" so với "v".
Tôi đang dựa vào Python và hiện đang làm việc trên nội dung OOP.
@StevenRumbalski: Tóm lại, có. Nhưng chỉ ở lối thông dịch. –
Mã đầu tiên không tạo ra ngoại lệ đó. Hiển thị toàn bộ dấu vết của bạn. (Correction: nó không tạo ra ngoại lệ đó trong Python 3.3 hoặc cao hơn. Trong 3.2 nó có.) – geoffspear
@Wooble Nah! Đó là những gì tôi đã mất tích .. – aIKid