Static methods and class member methods for Python

Source: Internet
Author: User

Both the static and class member methods of Python can be accessed by a class or instance, and the concepts are not easy to clarify, but there are still differences: 1) Static methods need not pass in the self parameter, class member methods need to pass in the CLS parameter representing this class, 2) from 1th, the static method is unable to access the instance variable, The class member method also has no access to the instance variable, but can access the class variable; 3) The static method is somewhat like the Function ToolPak, and the class member method is closer to the static method in the Java object-oriented concept. two ways to implement static methods and class methods first, in Python 2.3 and before, with the Staticmethod and Classmethod type Object wrapper implementationExamples are as follows (note the instructions in print): Class MyClass:
Val1 = ' Value 1 '
def __init__ (self):
Self.val2 = ' Value 2 '
Def staticmd ():
print ' static method, unable to access val1 and Val2 '
SMD = Staticmethod (staticmd)

def CLASSMD (CLS):
print ' class method, class: ' + str (CLS) + ', Val1: ' + cls.val1 + ', cannot access the value of Val2 '
cmd = Classmethod (CLASSMD)

Execution:>>> MC = MyClass ()
>>> MC.SMD ()
>>> Mc.cmd ()
>>> MYCLASS.SMD ()
>>> Myclass.cmd () second, in Python 2.4 and after, with the adorner (decorators) to achieveThe adorner uses the @ operator, as in the following example: Class MyClass:
Val1 = ' Value 1 '
def __init__ (self):
Self.val2 = ' Value 2 '

@staticmethod
Def staticmd ():
print ' static method, unable to access val1 and Val2 '

@classmethod
def CLASSMD (CLS):
print ' class method, class: ' + str (CLS) + ', Val1: ' + cls.val1 + ', cannot access the value of Val2 '

regardless of which of the above two ways, the execution is the same, in the way two executes the result as an example The analysis is as follows:Execute:>>> mc = MyClass () # instantiation
>>> Mc.staticmd () # instance calls a static method, cannot access instance variables Val1 and val2>>>
Static methods, unable to access val1 and Val2 >>> mc.classmd () # instances Call the class method, notice that the value of the variable val1 of the class MyClass is accessed here, not the instance variable of the MC after instantiation Val1, which is easy to confuse here, and you can see it down. Val2 is always an instance variable, so you cannot access the >>> class method, class: __main__. Myclass,val1:value 1, unable to access the value of Val2

The >>> myclass.staticmd () # class calls the static method directly, and the result is called with the above instance, whether it is a class variable or an instance variable that cannot be accessed >>>
Static methods that cannot access the VAL1 and Val2 >>> MYCLASS.CLASSMD () # classes call the class method directly, and the result is called with the above instance of the >>> class method, class: __main__. Myclass,val1:value 1, unable to access Val2 value >>> mc.val1 = ' value changed ' # Change the value of instance variable Val1

>>> MC.CLASSMD () # instance calls the class method, noting that the value of CLS.VAL1 does not change, so the cls.val1 at this time is the class variable val1, not the instance variable Val1>>> class method, class: __main__. Myclass,val1:value 1, unable to access the value of Val2
The >>> myclass.classmd () # class calls the class method directly, resulting in a call to the above instance of the >>> class method, class: __main__. Myclass,val1:value 1, unable to access Val2 value >>> myclass.val1 = ' class value changed ' # Change the value of class variable Val1
>>> MC.CLASSMD () # instance calls the class method, noting that the value of cls.val1 has changed, so it is further proved that the cls.val1 at this time is the class variable val1, not the instance variable Val1>>> class method, class: __ main__. Myclass,val1:class value changed, unable to access Val2 values >>> myclass.classmd () # class directly calls the class method, the result is called with the above instance of the >>> class method, class: __ main__. Myclass,val1:class value changed, unable to access val2 values Conclusion If the above process is too complex, keep in mind the following two points: static method:Unable to access class properties, instance properties, equivalent to a relatively independent method, and the class actually has nothing to do, in other words, in fact, is placed in the scope of a class function. class member methods:You can access class properties and cannot access instance properties. The above variable val1, which is a class variable in the class, is also an instance variable in the instance, so it is easy to confuse. Reprinted from: Static Methods and class member methods of Python

Static methods and class member methods for 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.