@classmethod的作用范围就是它接下来的那一个被定义的方法么?
@classmethod的作用范围就是它接下来定义的那一个方法么?
@classmethod的作用范围就是它接下来定义的那一个方法么?
2016-01-23
是,
class Person(object):
__count = 0
@classmethod
def how_many(cls):
return cls.__count
#@classmethod
def how_much(cls):
return cls.__count
def __init__(self,name):
Person.__count += 1
self.name = name
print Person.how_many()
p1 = Person('Bob')
print Person.how_much()
print Person.how_many()在how_many()下添加添加how_much()方法,运行到line 21会报错,如下
0 Traceback (most recent call last): File "D:\py\person_class.py", line 21, in <module> print Person.how_much() TypeError: unbound method how_much() must be called with Person instance as first argument (got nothing instead)
取消line 9的注释,代码正常运行
举报