Python--staticmethod (static method) and Classmethod (class method)

Source: Internet
Author: User

One, static method (Staticmethod)
    • The static method is to decorate the normal method as a static method through the @staticmethod adorner.
    • Static methods do not have to pass in the self parameter in a class and cannot access class variables and instance variables. Here is an example of a static method:
1 classClassName (object):2     """docstring for ClassName"""3     def __init__(Self, arg):4Self.arg =Arg5 6 @staticmethod7     deffoo ():8         Print('**************')9 TenA = ClassName (8) OneA.foo ()

The Foo method inside is decorated for a static method, and he cannot access class variables and instance variables that can be called.

When the instance is created, the instance itself is not automatically passed as a parameter to self, so the method does not have to write the argument to self, if the self is written, because the instance is not automatically passed to it, the error is that there should be a parameter but no incoming. This is what is said, static methods can not access the instance variables and class variables, if you want to not error, or honestly do not pass the self parameter, do a separate method, at this time, this static method and class is not much contact, but is equivalent to in this namespace. Another way is that he does not automatically pass the instance object to the self parameter, so we can put this example into human.

The above code is the first case, since it is not possible to access the instance variable class variables, do not pass parameters, do not access, the following is an artificial incoming object.

1 classClassName (object):2     """docstring for ClassName"""3     def __init__(Self, arg):4Self.arg =Arg5 6 @staticmethod7     deffoo (self):8         Print('**************', Self.arg)9 TenA = ClassName (8) OneA.foo (a)#when this static method is called, because it has a self argument, the object created above is passed to self A  -Classname.foo (a)#This code means that the static method can be called directly from the class name, and the normal method cannot be

Second, class method (Classmethod)
    • The method of decorating by @classmethod is a class method.
    • Class methods can access only class variables and cannot access instance variables.
    • When we directly access the initialized instance variable, we are prompted without this attribute (Attributeerror:type object ' ClassName ' has no attribute ' arg ').
1 classClassName (object):2     """docstring for ClassName"""3 4Name ='Pig' #This is a class property5 6     def __init__(Self, arg):#Init initializes the instance properties and instance methods, where only one property is written7Self.arg =Arg8     9 @classmethodTen     defFoo (CLS):#class method parameters are written as CLS by default One         Print('**************', Cls.name) A  -A = ClassName (8) -A.name ='Cat' #Here I change the name by an instance object, and the output can see that the Foo method does not change, confirming that the class method cannot access the instance variable the A.foo () -  -Classname.foo ()#Similarly, class methods can be called directly from the class name.
Iii. Summary
    • Normal methods cannot be called directly from the class name, while static methods and class methods can
    • Access:
      • Normal methods can access instance properties through self
      • Class methods can access class properties through the CLS, but cannot access instance properties
      • Static methods cannot access instance variables and class variables, and you must access the instance variables manually by passing in the instance object's method

Python--staticmethod (static method) and Classmethod (class method)

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.