Programming Xiaobai's self-study notes five (Python class method)

Series Article Directory

Programming Xiaobai's self-study notes 4 (regular expression module search function)

Programming Xiaobai's self-study notes three (Python regular expressions) 


Table of contents

foreword

1. Example method

Second, the class method

1. The first output result

2. The second and third output results 

(1) Add output statement

(2) Add parameter passing

Summarize


foreword

Python is an object-oriented programming language, and class is the most basic concept in it. A class can be seen as an abstract data type, including properties and methods. Python class methods include class methods, instance methods, and static methods, and methods can be dynamically created during runtime.


1. Example method

Instance methods of a Python class are methods bound to an instance of the class, and they are usually methods defined using a def statement within the class definition. Instance methods take a self parameter, which points to the instance itself. Through the self parameter, instance methods can access the properties and methods of the instance.

Here is an example class that demonstrates how to define and use instance methods:

class Dog:
    def __init__(self,n):
        self.name = n
    def jump(self):
        print(f'{self.name}跳')
    def run(self):
        print(f'{self.name}跑')

dog1 = Dog('小白')
dog1.jump()
dog1.run()

Here we can see that the jump() method and the run() method both pass in the self parameter. If you don’t write self here, you can write it in other words. I tested it personally and it works, but we still follow the convention. The result of the operation is:

little white jump

little white run

In summary, instance methods are a very important feature of Python classes, they allow interaction and communication between classes and objects.

Second, the class method

The class method is to add the "@classmethod" modifier to the method name, so that the instance name. method name and class name. method name can be accessed (under normal circumstances, the class name. method name will report an error), the class method is bound Methods that are bound to a class rather than an instance can be called from the class or from the instance. The first parameter of a class method is usually "cls", which refers to the class itself. You can use class methods to access class attributes and call other class methods. . The specific example code is as follows:

class Dog:
    name = 'xiaohuang'
    def __init__(self,name):
        self.name = name
    def jump(self):
        print(f'{self.name}跳')
    @classmethod
    def run(cls):
        print(f'{cls.name}跑')

dog1 = Dog('小白')
dog1.jump()
dog1.run()
Dog.run()

It is quite difficult for me to understand here. After thinking for a long time, the code has been modified repeatedly and finally runs successfully. The result of the operation is:

little white jump

xiaohuang run

xiaohuang run

 Let's analyze it together:

1. The first output result

The first output result is "Xiaobai running", which is not difficult to understand. Like the instance method, an instance dog1 is created first, and the "Xiaobai" parameter is passed in, and then the jump() method of dog1 is called to output the result. At this time, self.name is "Xiaobai".

2. The second and third output results 

 The result of dog1.run() turned out to be "xiaohuang ran", why not "xiaobai ran"? The result of my analysis is because of the addition of the "@classmethod" modifier. When dog1 calls the run() method, the system will call the __init__ method again, that is, create a new instance. At this time, because no parameters are passed in, So the default value "xiaohuang" was used, for which I experimented:

(1) Add output statement

I added an output statement print('call me once') in the __init__ method. According to the above guess, it should output three times "call me once", but the result is that only the first output is output, only once, The dog1.run() and Dog.run() methods do not call the __init__ method again. This proves that my guess above is wrong.

(2) Add parameter passing

Based on the failure of the above experiment, I guess again. If the __init__ method is not called again, the result will be because the run() method is called after the "@classmethod" modifier is added. cls represents the instance itself, then cls .name will re-call the name variable defined by the class, which has nothing to do with the __init__ method, so I added parameters to the run() method. The code is as follows:  

 def run(cls,name):       
 cls.name = name
 print(f'{cls.name}跑')

dog1 = Dog('小白')
# dog1.jump()
dog1.run('bbb')
Dog.run('aaaa')

 The output is:

bbb run

aaaa run

is consistent with my desired output, I should be right this time

Summarize

My personal understanding of the class method is not necessarily correct, and you are welcome to criticize and correct.

Guess you like

Origin blog.csdn.net/m0_49914128/article/details/131490062