Python static methods and class member Methods

Source: Internet
Author: User
Python's static methods and class member methods can be accessed by classes or instances. The two concepts are not easy to clarify, but there are still differences: 1) static methods do not need to pass in the self parameter, the class member method must be passed in to the cls parameter representing the class; 2) from the first 1st, the static method cannot access the instance variable, and the class member method cannot access the instance variable, but it can be used as class variables; 3) static methods are a bit like Function Tool libraries, while class member methods are closer to static methods similar to Java object-oriented concepts. Static methods and class methods I. Using staticmethod and classmethod type object packaging before Python 2.3The example is as follows (note the description 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 val2 value'
Cmd = classmethod (classmd)

Run:>>> Mc = MyClass ()
>>> Mc. smd ()
>>> Mc. cmd ()
>>> MyClass. smd ()
>>> MyClass. cmd () II. Implementation with decorators in Python 2.4 and laterThe decorator uses the @ operator, for 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 val2 value'

No matter which of the above two methods, the execution is the same, and the execution result is in method 2. Example The analysis is as follows:Run: >>> mc = MyClass () # instantiate
>>> Mc. staticmd () # The instance calls the static method and cannot access the instance variables val1 and val2 >>>
Static Method, unable to access val1 and val2

>>> Mc. classmd () # The instance calls the class method. Note that the value of val1, the variable of MyClass, is accessed here, instead of val1, the instance variable of mc after instantiation, which is easy to be confused here, you will understand it later. Val2 is always an instance variable, so it cannot be accessed >>> class method, class :__ main _. MyClass, val1: Value 1, and the Value of val2 cannot be accessed.

>>> MyClass. staticmd () # The class calls the static method directly. The result is called by the same instance as the preceding one. Both class variables and instance variables cannot be accessed. >>
Static Method, unable to access val1 and val2

>>> MyClass. classmd () # The class calls the class method directly. The result is the same as the preceding instance call >>> class method, class :__ main __. myClass, val1: Value 1, cannot access val2 Value >>> mc. val1 = 'value changed '# change the Value of the instance variable val1

>>> Mc. classmd () # The instance calls the class method and notices cls. the value of val1 is not changed, so the cls. val1 is the class variable val1, not the instance variable val1 >>> class method, class :__ main __. myClass, val1: Value 1, cannot access val2 Value
>>> MyClass. classmd () # The class calls the class method directly. The result is the same as the preceding instance call >>> class method, class :__ main __. myClass, val1: Value 1, cannot access val2 Value >>> MyClass. val1 = 'class Value changed '# change the Value of the Class variable val1
>>> Mc. classmd () # The instance calls the class method and notices cls. the value of val1 has changed, so it further proves the cls. val1 is the class variable val1, not the instance variable val1 >>> class method, class :__ main __. myClass, val1: Class Value changed, cannot access val2 Value

>>> MyClass. classmd () # The class calls the class method directly. The result is the same as the preceding instance call >>> class method, class :__ main __. myClass, val1: Class Value changed, cannot access val2 ValueConclusionIf the execution process is too complex, remember the following two points:Static Method:It is impossible to define class attributes and instance attributes. It is equivalent to a relatively independent method and has nothing to do with the class. In other words, it is actually a function in the scope of a class.Class member method:You can access the attributes of an instance by using the category attribute. The above variable val1 is a class variable in the class and an instance variable in the instance, so it is easy to confuse.

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.