#-*-Coding:utf-8-*-class A (object): Def im (self): "" "instance method: The generic method defined in the Pthon class is an instance method, which has an essential parameter
, self, which must be in the first position of the argument list, equivalent to the class that the method belongs to in the instantiated instance itself, (Python2 is an instance of the class, Python3 can make anything), and it can only invoke A () through an instance of the Class. IM () : return: "" Print ' Instance methods: ', self @classmethod def cm (CLS): "" class method: Need to
To an adorner (@classmethod), convert a normal method to a class method, which takes an implied argument as the first argument, which represents the class to which the incoming modification method belongs.
If the Classmethod is invoked in a derived class, the implicitly-passed argument is the subclass object, not the parent class. : return: "" Print ' Class methods: ', CLS @staticmethod def SM (): "" static method: Requires an adorner ( @staticmethod), converts a normal method to a static method: its arguments are any custom parameters, and of course there are no parameters, it does not require an implied parameter, it can be invoked directly by the class A.sm (), or it can be invoked on the instance, A (). SM () and instance is ignored, the actual or used class calls: return: "" "" Print ' Static method ' class B (A): pass If __name__ =
= ' __main__ ': a = A () a.im () a.cm () A.sm () print ' =========== ' B = B () b.im () b.cm () B.sm ()