Programming Xiaobai's self-study notes six (static methods and dynamic methods of classes in python)

Series Article Directory

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

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

Series Article Directory

foreword

1. Static method

1. Static methods can take no parameters.

2. The static method cannot get the instance

3. Static methods can pass other parameters 

2. Dynamic method

Summarize



foreword

Today we continue yesterday's learning and learn the methods of the python class. Classes in Python have two methods: static methods and dynamic methods, which have some different characteristics when used. Let's explain in detail:


1. Static method

A static method is to add a line of "@staticmenthod" modifier to the method name to designate the method as a static method. Does this English word mean static? In static methods, there is no need to use the self keyword to access instance variables.

A static method is a method defined in a class, which has nothing to do with the instance. The static method is consistent with the class method learned yesterday, and can be accessed through "instance name. method name" and "class name. method name". But the static method has no required parameters, and cannot directly get the calling instance and class. Static methods are often used to implement functionality associated with a class, but do not require access to instance variables or instance methods. How to understand this sentence? I first wrote the following code: 

class Dog:
    def __init__(self,n):
        self.name = n
    def getname1(self):
        print(f'小狗的大名是{self.name}')
    @staticmethod
    def getname2(self):
        print(f'小狗的小名是{self.name}')
dog = Dog('小白')
dog.getname1()
dog.getname2()

The ending is undoubtedly an error. The error code is getname2() missing 1 required positional argument: 'self', which means that the parameter self is not required, so I removed this parameter. The result after running is:

The puppy's name is Xiaobai

puppy's nickname is 

It can run normally, but there are no parameters, so I modified the code as follows: 

class Dog:
    def __init__(self,n):
        self.name = n
    def getname1(self):
        print(f'小狗的大名是{self.name}')
    @staticmethod
    def getname2(m):
        print(f'小狗的小名是{m}')
dog = Dog('小白')
dog.getname1()
dog.getname2('狗子')

 It works fine, the output is

The puppy's name is Xiaobai

The puppy's nickname is Gouzi 

 From this it can be concluded that:

1. Static methods can take no parameters.

Through experiments, it is found that without setting parameters, it can be called through an example, and the content will be output normally.

2. The static method cannot get the instance

In the example, no matter whether the self instance is passed in when defining the method, self.name cannot be called.

3. Static methods can pass other parameters 

My understanding is that a static method is like an ordinary method written in a class, and has little to do with this class. It is just written in a class, and it is no different from an ordinary function.

 Note: Static methods do not automatically pass any parameters, so we have to pass them explicitly when calling.

2. Dynamic method

Dynamic methods, as the name implies, are dynamic creation, which refers to dynamically adding methods to a class or instance at runtime. It is not created when the class is created, and all methods are not defined statically. That is to say, an existing method can be Passed to the class, the dynamic method is a regular method defined in the class, which is related to the instance and can be called through the instance. Dynamic methods are usually used to implement the functions of manipulating instances in a class. According to this idea, I wrote the following code:

class Dog:
    def __init__(self,n):
        self.name = n
        
def run(m):
    print(f'{m}快跑')
    
Dog.run = run
dog = Dog('小白')
dog.run()

 There is no error when running, but it is not the result of the code I want. The output is:

<__main__.Dog object at 0x000001FA3EA28100> run 

It is not difficult to find that m at this time is an example. Recalling the previous code, the method parameters in the class are all instances, which means that ordinary methods or functions cannot be directly assigned to the class, or the result you get is not what you want, so I changed the method to:

def run(m):

print(f'{m.name} run fast')

This time the result was correct: Xiaobai ran quickly.

In general, Python provides a variety of ways to implement the dynamic methods of classes, which makes Python highly flexible and extensible, and you can choose the most appropriate way according to your needs. 


Summarize

Both static methods and dynamic methods are methods defined in a class in Python, which are used to implement the functions of the class. Static methods have nothing to do with the instance and can be called directly using the class name; dynamic methods are related to the instance and need to be called through the instance. Static methods do not automatically pass any parameters and need to be passed explicitly when calling; dynamic methods automatically pass the instance as the first parameter to the method.

Guess you like

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