__init__ function usage

__init__ is a special method (special method) in a Python class, also known as a constructor. It is automatically called during class instantiation (object creation) to initialize object properties and perform other necessary settings.

The full name of the constructor is __init__(), note that there are two leading and trailing double underscores in its name, which is a naming convention in Python to indicate that it is a special method. Its definition is usually located in the class declaration and is used to describe the initial state of the class.

Example class and constructor:

class MyClass:
    def __init__(self, name, age=0):
        self.name = name
        self.age = age

# 创建对象实例,并传递必需参数
obj1 = MyClass("Alice")

# 创建对象实例,并传递必需参数和可选参数
obj2 = MyClass("Bob", age=30)

In the above example, the MyClass class has a constructor __init__ that accepts two parameters name and age, where age has a default value of 0. When you create the object obj1, you pass the required parameter name, and age will use the default value 0. When you created the object obj2, you passed the required parameter name and the optional parameter age. In the constructor, self.name and self.age are set to the passed values, thus initializing the properties of the object.

Example:

class Person:
    def __init__(self, name, age=0):#如果不声明age的值,默认为0
        self.name = name
        self.age = age

# 创建一个Person对象,传递name和age参数
person1 = Person("Alice") #默认age=0

# 创建一个Person对象,传递不同的name和age参数
person2 = Person("Bob", 25)

# 访问对象的属性
print(person1.name)  # 输出: "Alice"
print(person2.age)   #输出:"0"
print(person2.name)  # 输出: "Bob"
print(person2.age)   # 输出: 25

In the above example, we defined a Person class, where the constructor __init__ accepts two parameters name and age. When we create person1 and person2 objects, the constructor will set the passed parameters as properties of the objects. Afterwards, we can get and modify the state of the object by accessing its properties.

Let's take another example:

class EvalState:
    def __init__(self, image, depth=0):
        # 初始化EvalState对象时,传入图像(image)和深度(depth)两个参数,并将它们保存在对象的属性中
        self.image = image
        self.depth = depth
        self.stopped = False

Exactly as you said, this code defines a class called EvalState, and that class has a constructor __init__.

Let's explain this class and constructor further:

Class name: EvalState is the name of the class, which means to create the definition of the EvalState class. This class may be a data structure used to represent the evaluation status.

Constructor: init (self, image, depth=0) is the constructor of the EvalState class. The constructor is called automatically when an instance of the EvalState class is created, and it is used to initialize the newly created object.

Parameters: The constructor accepts two parameters: image and depth. Among them, image is a required parameter, which represents the image object (or image data) passed to the constructor. depth is an optional parameter and it has a default value of 0. An optional parameter means that you can choose whether to provide this parameter when instantiating the object, if not provided, it will use the default value.

Attributes: Inside the constructor, set the attributes of the object through self.image and self.depth. In this way, each instance of the EvalState class will have an image attribute and a depth attribute, corresponding to the values ​​​​of the image and depth parameters passed to the constructor.

Other properties: The constructor also sets a property called stopped and initializes it to False. This attribute may be used to represent some state of the object.

An example of creating an object using the constructor of this class:

class EvalState:
    def __init__(self, image, depth=0):
        # 初始化EvalState对象时,传入图像(image)和深度(depth)两个参数,并将它们保存在对象的属性中
        self.image = image
        self.depth = depth
        self.stopped = False

image_data=1
# 创建一个EvalState对象,传递image参数,使用默认的depth值(0)
state1 = EvalState(image_data)

# 创建一个EvalState对象,传递image和depth参数
state2 = EvalState(image_data, depth=3)

print(state1.image)# 输出结果为1
print(state1.depth)# 输出结果为0
print(state2.image)# 输出结果为1
print(state2.depth)# 输出结果为3

Summary:
The __init__ constructor is a special method of a Python class that is used to initialize the properties of an object when it is created. Through the constructor, we can receive externally passed parameters and use them to set the initial state of the object so that the object is in a usable state after creation. This way, we can access and manipulate the object's properties and methods immediately after the object is created.

Guess you like

Origin blog.csdn.net/thy0000/article/details/131969593