Python's Classmethod and Staticmethod differences

Source: Internet
Author: User

static method (Staticmethod)

Class method (Classmethod)

Both static methods and class methods can be accessed through the class name. Method name or instance. Method Access.

#-*-Coding:utf8-*-                                                                                                                                     class A (object):    def instance_method (self,x):        print "Instance_method (%s,%s)"% (self , x)    @classmethod    def class_method (cls,x):        #类方法的调用使用类本身作为其隐含的参数        #调用者本身并不需要显示提供该参数        print " Class_methodi (%s,%s) "% (cls,x)    @staticmethod    def static_method (x):        #静态方法没有常规方法的特殊行为        print" Static_method (%s) "% (x) a = A () a.instance_method (' Test ') a.class_method (' Test ') a.static_method (' Test ')

  

Output Result:

Instance_method (<__main__. A object at 0x7f4b7beacd90>,test) Class_methodi (<class ' __main__. A ' >,test) static_method (test)

  

Python's Classmethod and Staticmethod differences

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.