classmethod Class method

1) stay python in . Class method @classmethod It's a function modifier , It means that next is a class method , What we usually see is called instance method . The first parameter of the class method cls, The first parameter of the instance method is self, Represents an instance of this class .
2) Normal object methods need at least one self Parameters , Represents an instance of a class object
3) Class methods have class variables cls Pass in , So that you can use cls Do something about it . And subclass inheritance , When calling this class of methods , Class variable passed in cls Subclass , Not the parent . For class methods , You can call... Through a class , It's like C.f(), It's kind of similar C++ Static methods in , You can also call... Through an instance of a class , It's like C().f(), here C(), When it's written like this, it's an instance of a class .

 class info(object):
@classmethod
def sayclassmethod(cls): print 'say %s' % cls def saymethod(self): print 'say %s' % self test = info()
test.saymethod()## Instance call method
test.sayclassmethod()## Instance calls class methods
info.saymethod(test)## Class calls instance methods
info.sayclassmethod()## Class calls class methods

python Class method and class call instance method

  1. Python Class method 、 Static method and instance method ----- There is no need to instantiate parameters in a class And No self Function of The method that calls this function

    source : https://www.cnblogs.com/blackmatrix/p/5606364.html A static method is a method that can be called in a class without the participation of an instance ( Unwanted self Parameters ), During the call , There is no need to implement the class ...

  2. Python Class method 、 Static method and instance method

    A static method is a method that can be called in a class without the participation of an instance ( Unwanted self Parameters ), During the call , There is no need to instantiate the class , Use directly after the class . Operator to call method . Usually , The static method uses @staticmethod Adorner to declare . ...

  3. python Class methods call each other and self The meaning of

    Every time an internal method is called , Method add self.   class MyClass: def __init__(self): pass def func1(self): # do something pri ...

  4. python Class method 、 Class properties and static methods

    class Game(object): # Class properties num = 0 # Example method def __init__(self): # Instance attributes self.name = "laowang" # Quasi square ...

  5. python Class method 、 Static methods 、 Example method example

    Class method , Static methods , Common method #coding=utf-8   class Foo:     def __init__(self,name):         self.name=name       d ...

  6. Python Study : Classes and instances

    Python Study : Classes and instances The author of this article : Xuanhun studio -- Hot ants class , In learning object-oriented, we can treat class as a specification , This idea is my personal experience , It's important to feel , In addition to the encapsulated functions , Class as a specification , We can customize our own rules ...

  7. Python Interview questions Python Classes and instances in

    0x00  Preface class , In learning object-oriented, we can treat class as a specification , This idea is my personal experience , It's important to feel , In addition to the encapsulated functions , Class as a specification , We can customize our own specifications , From this point of view , When we learn design patterns later , ...

  8. python Four ways to modify class variables , Instance objects call class methods to change the values of class properties , Class objects call class methods to change the values of class properties , Call the instance method to change the value of the class property , Directly modify the values of class properties

    Three ways to modify class variables , Instance objects call class methods to change the values of class properties , Class objects call class methods to change the values of class properties , Call the instance method to change the value of the class property , The class name is the class object ,city Is a class variable , #coding=utf-8 class empl ...

  9. python class \ Static methods \ Class method distinction parsing ( The attached code )

    Preface It's been a long time python, I haven't been very clear about these kinds of methods , Take the time to test it today , It's kind of a rough idea . The difference between the three methods Take a look at the picture first You can see , The instance can be called by all three methods , Classes can only call two . therefore ...

Random recommendation

  1. poj2612Mine Sweeper

    It's a very simple simulation , But in the team, it's always WA 了 10 Hair ... I ca I don't understand the title , Nor does it count. , I thought he'd mark the star when he touched the mine (*) , But it's still (x),T_T #include <cstdio> #in ...

  2. Mysql Task scheduling

    Mysql Task scheduling Event Scheduling configuration Mysql Task scheduling Event Don't execute Mysql Task assignment Event Don't execute The way I do it is : Method 1 : Find the currently used .cnf file , stay [mysqld] Below ...

  3. About cocoapods Add the wonderful configuration of static library

    Not much to say , Go straight to the code When this static library is introduced , At first, I couldn't find this static library when editing . Until I see this post :http://stackoverflow.com/questions/19189463/cocoapod ...

  4. SignalR: The new old thing

    As you can see, this is my first blog posted in cnblog. If you find any mistake, don’t hesitate to t ...

  5. Windows Next chm Convert to html It's a super simple way to

    Abstract : By calling Windows command , take chm The file is converted to html file summary : Many programmers will encounter such problems , Look at an offline version of the help document (chm file ), There will always be an index file ( Of the document chw file ), And there are ...

  6. HR_Two Strings

    https://www.hackerrank.com/challenges/two-strings/problem?h_l=interview&playlist_slugs%5B%5D=int ...

  7. python Color as defined in

    In daily study and work , We often come into contact with some big guys writing Python Utilities , It always shows colorful background when it runs , The relevant definition is matplotlib Module , For ease of use , Let's show you which colors are defined in this module ...

  8. ( turn )WebSphere Of web How to get the data source in the project

    original text :http://aguu125.iteye.com/blog/1694313 https://blog.csdn.net/bigtree_3721/article/details/44900325 ...

  9. UVM Medium regmodel modeling ( One )

    UVM Medium regmodel Inherited from VMM Of RAL(Register Abstract Layer), Now we can start with the register model XML modeling , Re pass Synopsys The tools of home ralgen To directly generate regmo ...

  10. 20145311 《Java Programming 》 Week 7 learning summary

    20145311 <Java Programming > Week 7 learning summary Summary of the learning content of the textbook Chapter 12 Lambda Lambda Expressions make programs more concise , In parallel design , Be able to do parallel processing . Chapter 13 Time and ...