This is the 22nd day of my participation in the More text Challenge. For details, see more text Challenge

An overview of the

A class is an abstract template for an object.

The __init__ constructor is one of Python’s magic methods, as shown in the magic method

We use the class template to create an instance object of the class and then call the functions defined by the class.

How are the properties of an instance object initialized?

This is where Python introduces the __init__ constructor

Constructor, which Python executes automatically after the instance object is created, to put the initialized property characteristics into the instance object.

1. Introduction to constructors

A Python object has three parts: id, type, and value.

So let’s dig deeper into the three parts that an object contains:

  1. Id, the storage address of the object in the heap memory

  2. Type (object type), Python’s six data types

  3. Value (the value of the object)

    (1) attribute (the attribute)

    (2) the method (method)

After we create an instance object from the class, we need to define the constructor __init__() method.

The constructor is used to initialize an instance object. After the object is created, it initializes the properties of the current object and returns no value

Constructor key:

  1. Fixed name, must be __init__()
  2. The first argument is fixed. It must be self. Self refers to the instance object that you just created.
  3. Constructors are usually used to initialize the instance properties of an instance object
  4. The constructor is called with the class name (argument list), and when called, the created object is returned to the corresponding variable
  5. The constructor __init__() method: initializes the created object by assigning values to instance attributes
  6. Create an instance __new__() method: used to create an object, but it is generally not necessary to redefine the method

2. The underlying mechanism of constructors

We learned the constructor process through chestnuts

Class Animal: def init__(self,name,food): self.name = name self.food = food cat = Animal("Tom","fish")Copy the code

The constructor initializes the instance object as follows:

1. The Animal class creates a space in the heap for instance objects using the default __new__() method

  1. Once an instance object is created, Python automatically calls the __init__() method to create memory for the object’s data members

3. Complete the initialization of the object data members, and assign the corresponding instance attribute variable to each attribute value

Knock on the blackboard, here comes the important point

  • Self in Python is the equivalent of the self pointer in C++ and the this keyword in Java and C#.
  • In Python, self must be the first argument to the constructor, and the name can be changed at will, but the general convention is to call it self
  • The constructor can only be called once, after new has created the object

3. Constructor operation

Class Animal: def init__(self,name,food): self.name = name self.food = food def info(self): Print (self.name,' my favorite food is ',self.food) # create cat = Animal("Tom","fish") # create cat = Animal("Tom","fish") # create dog = dog Animal("Bob","meat") dog.info()Copy the code

Development:

  • If the subclass’s __init__() function is not overridden, then the superclass constructor is found along the search tree to execute the superclass constructor

  • If a subclass overrides a constructor, it executes its own constructor method after creating an instance object. The parent class’s constructor does not

  • A subclass overrides the constructor, which can explicitly call the constructor form of the superclass as follows:

    Parent class. __init__ (self)Copy the code

conclusion

We learned today that the constructor __init__() is called automatically by Python after the object is created to initialize the instance object property data. It returns no value, and the constructor cannot be displayed.

When creating an object, the constructor can take arguments, if needed. When you create a class without a constructor, Python automatically creates a default constructor that does nothing.

Every class must have a constructor, even if it relies only on the default constructor

Ok, above is the content of this issue, welcome big guy comment area correction ~