The use and difference of class method, class instance method, static method in Python

Source: Internet
Author: User

<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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.