Classmethod and Staticmethod - Python 类方法 和 静态方法
1 classmethod and staticmethod
2 classmethod 的是一个参数是类对象 cls (本类,或者子类), 而不是实例对象 instance (普通方法). classmethod 即可以通过'类'调用 - cls.classfunc(), 3 也可以同通过实例调用('The instance is ignored except for its class')- instance.classfunc() / cls().classfunc() 4 当通过'子类'调用'基类'的 classmethod 的时候, '子类'的类对象被当做第一个参数处理. 5 'When a class attribute reference (for class C, say) would yield a class method object, 6 it is transformed into an instance method object whose __self__ attributes is C. ' 7 8 举个例子, 9 class A(object): 10 11 @classmethod 12 def func(cls): 13 print(cls) 14 print('A - classmethod') 15 16 class B(A): 17 pass 18 19 if __name__ == "__main__": 20 A.func() #1 通过本类的'类对象'调用 classmethod 21 abc = A() 22 abc.func() #2 通过本类的'实例对象'调用 classmethod 23 B.func() #3 通过子类的'类对象' 调用 classmethod 24 bcd = B() 25 bcd.func() #4 通过子类的'实例对象'调用 classmethod 26 27 Output, 28 <class '__main__.A'> #5 classmethod 的第一个参数是 '类对象' 29 A - classmethod 30 <class '__main__.A'> 31 A - classmethod 32 <class '__main__.B'> #6 通过'子类'调用'基类'的 classmethod 的时候, '子类' 的类对象被当做第一个参数处理 33 A - classmethod 34 <class '__main__.B'> 35 A - classmethod 36 37 staticmethod 的第一个参数不在是'特殊参数'(cls 类本身, 或 self 实例), 可以将 staticmethod 理解为定义在类定义提中的普通函数. 38 staticmethod 提供了一个将 function objects 转换成 method objects 的方式. staticmethod 本身是不可调用的(not callable), 39 然而通过 40 staticmethod 即可以通过'类'调用 - cls.staticfunc(), 41 也可以同通过实例调用('The instance is ignored except for its class')- instance.staticfunc() / cls().staticfunc() 42 43 'When it would yield a static method object, it is transformed into the object wrapped by the static method object' 44 45 例子, 46 class A(object): 47 48 @staticmethod 49 def func(): 50 #print(callable(A.func)) 51 print('A - staticmethod') 52 53 class B(A): 54 pass 55 56 if __name__ == "__main__": 57 A.func() #1 通过本类的'类对象'调用 staticmethod 58 abc = A() 59 abc.func() #2 通过本类的'实例对象'调用 staticmethod 60 B.func() #3 通过子类的'类对象' 调用 staticmethod 61 bcd = B() 62 bcd.func() #4 通过子类的'实例对象'调用 staticmethod 63 64 Output, 65 A - staticmethod 66 A - staticmethod 67 A - staticmethod 68 A - staticmethod 69 70 Static method objects 71 Static method objects provide a way of defeating the transformation of function objects to method objects. 72 A static method object is a wrapper around any other object, usually a user-defined method object. 73 When a static method object is retrieved from a class or a class instance, the object actually returned is the wrapped object, 74 which is not subject to any further transformation. Static method objects are not themselves callable, 75 although the objects they wrap usually are. 76 Static method objects are created by the built-in staticmethod() constructor. 77 It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. 78 If a class method is called for a derived class, the derived class object is passed as the implied first argument. 79 80 Class method objects 81 A class method object, like a static method object, is a wrapper around another object that alters the way 82 in which that object is retrieved from classes and class instances. 83 Class method objects are created by the built-in classmethod() constructor. 84 It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. 85 86 *** 注, decorator 返回的是可调用的函数或方法对象. 87 通常 classmethod 和 staticmethod 是通过装饰器 @classmethod 和 @staticmethod 实现的.