Python_ Built in method

Keywords: Python

;

1. Life cycle of del methods and objects

Introduce the built-in methods of two objects. In Python, there is another initialization method__ init__ Corresponding methods__ del__ Method. Before an object is to be destroyed from memory, the so-called destroy from memory is to click the object from memory. Before the object leaves memory, the system will automatically call the object for us__ del__ method.

In actual development, ah, you can__ init__ Method, which can make the creation of objects more flexible, right? In development, if you want an object to do something before it leaves the world, you can consider it at this time__ del__ To customize the initialization method, you should add a comma after self, and then add a formal parameter, new_name, after the formal parameters of the method are ready, we define a name attribute inside the method and put new_ The name parameter is set to the name attribute,

After the attribute definition is completed, we use the print function to make an output. Now a custom initialization method has been completed,

Use the class name to create the object, and specify new when creating the object_ Name is the argument corresponding to this parameter, tom = Cat("Tom"),  

class Cat:
    def __init__(self, new_name):
        self.name = new_name
        print("%s coming" % self.name)

tom = Cat("Tom")

Run the following program and the result is:

After calling the initialization method, the object will survive in the world. Later, we can access the internal properties of the object through the variable name tom. The following code accesses the name property of tom

class Cat:
    def __init__(self, new_name):
        self.name = new_name
        print("%s coming" % self.name)

tom = Cat("Tom")
print(tom.name)

Running result: Tom is output

 

  No active call__ del__ Method, but it will be called automatically before the program execution is completed

class Cat:
    def __init__(self, new_name):
        self.name = new_name
        print("%s coming" % self.name)

    def __del__(self):
        print("%s to pass away" % self.name)

tom = Cat("Tom")
print(tom.name)
# Before the execution of the program is completed, it calls del__ Method, outputting "Tom has gone"

  Operation results:

 

Because this method is automatically called by the system before the object is destroyed from memory, now use the print function to make a split line at the bottom of the code, and "tom is gone" should be output below the split line, because tom is a global variable in the current module. After all our codes are executed, The system will recycle the object tom

class Cat:
    def __init__(self, new_name):
        self.name = new_name
        print("%s coming" % self.name)

    def __del__(self):
        print("%s to pass away" % self.name)

tom = Cat("Tom")
print(tom.name)
# Before the execution of the program is completed, it calls del__ Method, outputting "Tom has gone"
print("-" * 50)

Operation results:

 

Python has a special keyword del, which can delete an object. The Del keyword can delete an object. Now use the Del keyword to delete the Tom object. The code of the Del keyword is above the split line. Run the program again to compare the effect. This time, "Tom is gone" is output above the split line, Therefore, after we delete the Tom object with the Del keyword, the system will automatically call us first__ del__ Method, when__ del__ After the method is executed, the line code of print split line in line 20 will be executed

class Cat:
    def __init__(self, new_name):
        self.name = new_name
        print("%s coming" % self.name)

    def __del__(self):
        print("%s to pass away" % self.name)

tom = Cat("Tom")
print(tom.name)
# Before the execution of the program is completed, it calls del__ Method, outputting "Tom has gone"


# del keyword can delete an object
del tom
print("-" * 50)

Execution results:

When__ del__ After the method is executed, the line code of print split line in line 20 will be executed

 

2. str method customizes variable output information

Another built-in method__ str__, In Python, if we use the print function to output an object variable, it will be output by default. The object referenced by this variable is the object created by which class, and the memory address of this object will be output. If we use the print function to output this variable in development, we don't want to see these contents, If you want to see some content defined by yourself, you should use it__ str__ This built-in method, but it is in use__ str__ This built - in method must return a string

We directly output the variable tom on the console. If we run the program now, the Cat() and the memory address of the cat object tom will be output

The following code prints the tom variable directly

class Cat:
    def __init__(self, new_name):
        self.name = new_name

        print("%s coming" % self.name)

    def __del__(self):
        print("%s to pass away" % self.name)

#tom is a global variable
tom = Cat("Tom")
print(tom)

Output result (the third line): output the Cat() class and output the address of the object tom

This is the effect of using the print function by default

Now use__ str__ Method and returns a string

class Cat:
    def __init__(self, new_name):
        self.name = new_name

        print("%s coming" % self.name)

    def __del__(self):
        print("%s to pass away" % self.name)

    def __str__(self):
        #You must return a string
        return "I'm a kitten"

#tom is a global variable
tom = Cat("Tom")
print(tom)

Output results:

 

Added one__ str__ After the method, look at the effect. After "Tom is here", the output is "I'm a kitten", rather than the default class that created the object and the address of the object in memory. Through this drill, we can find that if we want to print the variables of an object using the print function during development, we want to see some customized information on the console, You can use it__ str__ This built-in method. At the same time, note that this method must return a string. Now that it returns a string, we can modify "I'm a kitten"

class Cat:
    def __init__(self, new_name):
        self.name = new_name

        print("%s coming" % self.name)

    def __del__(self):
        print("%s to pass away" % self.name)

    def __str__(self):
        #You must return a string
        return "I'm a kitten[%s]" % self.name

#tom is a global variable
tom = Cat("Tom")
print(tom)

Operation results:

Output "I'm Tom Kitten". Now, after modifying the usage method and printing the cat object Tom with the print function, you can directly see the value of the internal attribute of the cat object Tom, instead of checking the value saved by the name attribute in this object through tom.name

Posted by breadcom on Fri, 03 Sep 2021 16:56:51 -0700