<title>The use and difference of class method, class instance method, static method in Python</title> The use and difference of class method, class instance method, static method in Python
2011-03-24 11:58:06| Category: Python | Tags: Python class method static method | report | Font size Subscription
Transfer from http://stonelee.javaeye.com/blog/726449
How to use: Python Code Collection Code class A (object): Def foo (self,x): #类实例方法 print "executing foo (%s,%s)"% (self,x) @classmethod def class_foo (cls,x): #类方法 print "executing class_foo (%s,%s)"% (cls,x) @staticmetho D def static_foo (x): #静态方法 print "executing static_foo (%s)"%x
Call method: Python code collection Code A = A () A.foo (1)//print:executing foo (<__main__. A object at 0xb77d67ec>,1) A.class_foo (1)//executing Class_foo (<class ' __main__. A ' >,1) A.class_foo (1)//executing Class_foo (<class ' __main__. A ' >,1) A.static_foo (1)//executing Static_foo (1) a.static_foo (1)//executing Static_foo (1)
The difference: Both class and static methods can be called by class and class instances, and class instance methods can only be called by class instances where the implied invocation parameters of a class method are classes, whereas the implied invocation parameters of a class instance method are instances of the class, and static methods do not implicitly invoke parameters
From for notes (Wiz)
The use and difference of class method, class instance method, static method in Python