Detailed explanation of (__init__, __str__, __del__) functions in Python

Use of functions in Python classes and objects

1、__init__函数使用
2、__str__函数使用
3、__del__函数使用



foreword

Python is an object-oriented programming language.

Almost everything in Python is an object, with properties and methods.

Classes are similar to object constructors, or "blueprints" for creating objects.


one,__init__函数

When the class is called, the first step is to create an instance object. Once the object is created, the python interpreter will call the __init__() method in the class to add some predefined attributes to the instance object. If this method is not defined in the class, The python interpreter will look in the parent class of this class, and if the parent class does not have it, it will call the default __init__() method.
In a popular sense, it is an initialization definition inside the class.

1. Routine

class Demo:
    def __init__(self, num):
        self.num = num

    def add(self):
        self.num += 1
        print(self.num)


add_num = Demo(40)
add_num.add()

insert image description here

2. Explain

Here 40 is the num passed to the init function. And all initialization definitions are executed in the init function. In this example, we mainly initialize and define the interior of the Demo class, which mainly relies on the initial operation of the function to be transferred from the outside to the Demo class, and the internal initialization of the function can also be performed. as follows

class Demo:
    def __init__(self):
        self.num = 90

    def add(self):
        self.num += 1
        print(self.num)


add_num = Demo()
add_num.add()

insert image description here

ps: The self function name can only be used inside the class, which is equivalent to a local variable, and an error will be reported if it is used outside, as shown in the figure below
insert image description here

two,__str__函数

When using print to output an object, as long as you define the __str__(self) method, the data returned from this method will be printed

1. Routine

class Demo:
    def __init__(self):
        self.num = 90

    def __str__(self):
        return "例程的数字大小是[%s]" % self.num

    def add(self):
        self.num += 1


add_num = Demo()
add_num.add()
print(add_num)

insert image description here

2. Explain

As long as the object is printed outside the class, the str function will return the code representation behind the return keyword to the print. If there is no str function, let's see what happens, as shown in the following routine.

class Demo:
    def __init__(self):
        self.num = 90

    def add(self):
        self.num += 1


add_num = Demo()
add_num.add()
print(add_num)

insert image description here
The address of the Demo class object is printed here, so if there is no str function, only the object address will be printed

three,__del__函数

When the reference count of an object is 0, the __del__() method will be called automatically to release the memory occupied by the object. Sometimes we can rewrite this method to return some information when deleting the object to achieve some effects (like the game It is similar to the kill information returned when killing a hero in the middle.
The del function can be understood as a function that exerts residual heat, that is, when a life cycle is completed, this function will be called.

1. Routine

class Demo:

    def __init__(self, name):
        self.name = name

    def mz(self):
        print("这个人是[%s]" % self.name)

    def __del__(self):
        print("[%s]生命周期完了" % self.name)

    def __str__(self):
        return "%s" % self.name


tom = Demo("Tom")
tom.mz()
print(tom)

Jerry = Demo("Jerry")
Jerry.mz()
print(Jerry)

print("-" * 50)

insert image description here

2. Explain

It can be seen from print("-" * 50)the output that __del__the function is called after the entire function code has run. That is, the del function is called before an object is destroyed. To put it simply, the del function will be called when the code is fully executed. At the same time, you can also use the del function to delete an object directly outside the class.
insert image description here
The output is as follows
insert image description here

Summarize

Classes and objects are indispensable for developers to develop large-scale applications, so it is very important to make good use of built-in methods

Guess you like

Origin blog.csdn.net/qq_45156021/article/details/124364174