On python basics 09

The role of Self: Represents the object that calls the method. This parameter is automatically passed by the interpreter. # The calling object can be used for whatever it is. The first formal parameter does not have to be called self, but the variable name can be called any name. #But because the first parameter of the method must receive the object on which the method is called, it is customary to use self as the name
Magic method: it can run automatically at a specific time
init() method#Handle property initialization, assignment magic method Features: can run automatically when the object is created#As long as the method is defined in the class, it will be automatically executed at the right time#The method will be automatically executed after the object is created# Attributes generally used to initialize objects 1. Define a class 2. Create an object 3. Execute the __init__ method 4. Assign the object name #Every time a new object is created, the __init__ method will be executed #__init__ can be received after self during initialization Custom formal parameters#In the parentheses of the created object, the actual parameters passed will be automatically passed by the interpreter to the __init__ method. Differences between attributes and global variables: 1. Attributes can be used independently between objects without interfering with each other 2. Global variables can be shared among all methods, but there is only one program, which is easy to confuse the str() method: When printing an object, the __str__ method of the object is automatically called to return the description information of the object. The __str__ method must have a return value , and the return value must be a string. The return value will not give you a memory address. The del() method: After the program ends, when the object is destroyed, it will automatically execute the __del__ method. The last reference to the object is deleted, the object Will be destroyed. At this time, the __del__ method is automatically executed. The __del__ method can be used to verify whether the object is destroyed. The __del__ method can be used to release the resources occupied by the object and close the file.
Data Protection: Set get protect data
Private property: When defining a property, add two underscores before the property name, then the property is a private property and can only be used within its class. Private properties cannot be used outside of a class. #Assigning a private property outside the class actually adds a new property.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324603808&siteId=291194637