请提供此Class方法示例的支持


class Person:
number_of_people = 0
def __init__(self,name):
self.name = name
Person.add_person()
@classmethod                        
def number_of_people_(cls):         
return cls.number_of_people()
@classmethod
def add_person(cls):
cls.number_of_people += 1
p1 = Person('Tim')
p2 = Person('Jill')
print(Person.number_of_people_())

上面的代码给出

TypeError: 'int' object is not callable

请帮忙!

您的TypeError是因为您试图在classmethod:中返回什么

umber_of_people = 0
def number_of_people_(cls):         
return cls.number_of_people() # you are calling the attribute above which is set to 0

更改为:

def number_of_people_(cls):         
return cls.number_of_people

最新更新