This article mainly introduces the analysis of init instance method of Python constructor. The example code is introduced in detail in this article, which has certain reference value for your study or work. You can refer to the following for your friends
1、 To create an object, we need to define a constructor__ init__ () method. The constructor is used to initialize the instance object, that is, after the object is created, it initializes the properties of the current object without a return value.
Wei init__ () the main points are as follows:
1. Fixed name, must be__ init__ ()
2. The first parameter is fixed and must be self. Self refers to the sample object just created.
3. The constructor is usually used to initialize sample properties. The following code is to initialize sample properties:
4. Call the constructor through the class name (parameter list). After calling, the created object will be returned to the corresponding variable.
5.__ init__ () method: initialize the created object. Initialization means: “assign value to instance property”
6.__ new__ () method: used to create objects, but we generally do not need to define this method.
Instance properties are properties that are subordinate to instance objects, also known as instance variables. His use has the following points:
1. The instance properties are generally in the__ init__ () method is defined as follows:
Self. Instance property name = initial value
2. In other example methods of this class, access is also made through self:
Self. Instance property name
3. After creating an instance object, access it through the instance object:
Obj001 = class name () ා create object, call__ init__ () initialize properties
Obj01. Instance property name = value ා you can assign values to existing properties or add new properties
An instance method is a method that is subordinate to an instance object. The definition format of the instance method is as follows:
Def method name (self, [, formal parameter list]):
The call format of the method is as follows:
Object. Method name ([argument list])
1. When defining the example method, the first parameter must be self. As before, self refers to the current instance object.
2. When calling an instance method, it is not necessary or possible to pass parameters to self. Self is automatically passed by interpreter.
Differences between functions and methods:
1. They are used to complete a function of the statement block, the essence is the same
2. When the method is called, it is called through the object. Methods are subordinate to specific instance objects, and ordinary functions do not have this feature.
3. Intuitively, when defining a method, you need to pass self instead of function
The essence of method call of instance object:
s.write_code() == Student.write_code()
1. Dir (obj) can obtain all properties and methods of an object
2.obj.__ dict__ Object’s property dictionary
3. Pass statement
4. Isinstance (object, type) judge whether “object” is “specified type”
The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.