class Person:
count = 0
@classmethod
def init(cls):
cls.count += 1
a1=Person
a2=Person
a3=Person
print(Person.count)
Python中的classmethod(和staticmethod)并不止拥有美学上(或者命名空间上)的意义,而是可以实际参与多态的、足够纯粹的OOP功能,原理在于Python中类可以作为first class的对象使用,很大程度上替代其他OOP语言中的工厂模式。classmethod既可以作为factory method提供额外的构造实例的手段,也可以作为工厂类的接口,用来读取或者修改工厂类本身。