Classmethod difference in Python and the staticmethod

Learning python often there will be some similar or similar syntax module etc., we need comparative analysis to enhance memory, proficient.

staticmethod: static methods

classmethod: class method

In python, static methods and class methods are accessed through the class object can be instantiated and class objects. But the difference is:

@classmethod is a function modifier, it means that the following is a class method, but usually we see for example the method is called. The first parameter cls class method, the first parameter is example of a method of self, showing such an example.
Common Object process requires at least a self parameter representative of the class object instance of
the class methods cls incoming class variables, can be used to do some processing related cls. And when there is a subclass inheritance, when calling such methods, the incoming class is a subclass of cls variable, rather than the parent class. For class methods can be invoked through the class, as Cf (), a little static methods in C ++ similar, can also be called via an instance of a class, like C (). F (), where C (), write after such an instance it is the class.
The method is not static, it is substantially identical with a global function, rarely used in general.

 

1. In general, you want to use a class, you need to instantiate an object and then call the method .

Or use @staticmethod @classmethod, may not be required to instantiate, direct the class name. Method name () is invoked.

This facilitates the organization code, the function should belong to a certain class to go into that class, and help clean namespace.

 

2. Since @staticmethod and @classmethod can direct the class name. Method name () is invoked, and that they have what difference does it

From the point of view of their use,

  • @staticmethod itself does not need to represent objects and their own self cls class parameters, just use the same function.
  • @classmethod you do not need self argument, but the first parameter is a required parameter cls class of its own.

If @staticmethod to be called to some of the properties of this class of methods, you can only direct the class name. Property name or class name. Method name .

Since the holding @classmethod cls parameter may be invoked class attributes, class methods, instance objects and the like, to avoid hard-coded.

 

. 1  # - * - = UTF-Coding. 8 - * - 
2  # @time: 2019/12/02 19:51 overtime study 
. 3  # @Software: PyCharm 
. 4  # @author Jacky 
. 5  
. 6  class Foo (Object):
 . 7  
. 8      # examples of the method, the first parameter is an instance of an object. General practice of using self. 
. 9      DEF instance_method (Self):
 10          Print ( " a {} class instance method, the instance of the object can only invoke " .format (Foo))
 . 11  
12 is      # class method, the first parameter must be a class object. Generally accustomed to using cls. Use @classmethod decorator decoration. 
13 is  
14      @classmethod
 15      DEF class_method (CLS):
16          Print ( " is a class method " )
 . 17  
18 is  
. 19      # static method, the parameter is not required, and no binding relationship classes, it is a common method used @staticmethod decorator decoration. 
20 is      @staticmethod
 21 is      DEF static_method ():
 22 is          Print ( " static method " )
 23 is  
24  
25 foo = Foo ()
 26 is  
27  
28  # instance method can only be invoked Example 
29  foo.instance_method ()
 30  Print ( ' === ======= ' )
 31 is  
32  #Class class or instance method may be invoked. 
33 is  Foo.class_method ()
 34 is  foo.class_method ()
 35  
36  Print ( ' ---------- ' )
 37 [  
38 is  # static class or instance method may be invoked. 
39  Foo.static_method ()
 40  foo.static_method ()
 41 is  
42 is  
43 is  ' '' 
44 is  a class of <class' __main __. Foo ' > instance, it can only be called an object instance
 45  ==========
 46  is a class method
 47  is a class method
 48  ----------
 49  is a static method
 50  is a static method
 51  '' '
View Code

 

summary:

Classicmethod understood and staticmethod class is a data structure, an object can be created. When you call the class creates an instance of a class object. Once an object is created, python checks whether to implement the init () method. If init () has been implemented, it is called, the object instance is passed as the first argument (self).

 

Guess you like

Origin www.cnblogs.com/pythonbetter/p/11972913.html