I. Concepts of classes and objects.
What is a class
Class is the combination of a class of things with the same characteristics and actions, such as "Human" is a class.
What is an object
An object is a concrete thing that is created from a class that contains the characteristics and actions of the classes that are created.
What does a class have to do with an object all objects are generated by a class if you make a pen, the first thing you have to do is create a pen, and God makes a man. This template is a human "class" and then a pen and a person are generated according to the definition of the class.
What is instantiation
The process of producing an object from a class is the result of instantiating the class instantiation, which is also known as an instance of an Object object.
Two. Basic knowledge of the class.
1. How to create a class
#-*-Coding:utf-8-*-
A class is defined using the class syntax
Class Person:
Pass
Instantiate an object with the person class.
Ayumi is an object that is instantiated from the person class.
Ayumi = person ()
2. What is classic class and modern class
In the python2.x version, classes are divided into two types, the classic and the new, and none of the classes in Python3 or more are modern classes.
The biggest difference between classic and modern classes is that the new class must inherit a parent class without specifying the parent class in time and must inherit a parent class named Object
3. About the properties of the class.
Adds a property to a class.
#!/usr/bin/python2.7
#-*-Coding:utf-8-*-
Class Person:
Pass
Person.career = "Diva"
Print Person.career
>>>diva
#删除类中的一个属性.
Del Person.career
About some of the built-in methods in a class.
__init__ Construction method The code under the __init__ method is automatically executed when a class is used to generate an object.
In general, you can use construction methods to create attribute characteristics for each object when you build the object.
#!/usr/bin/python2.7
#-*-Coding:utf-8-*-
Class Person:
def __init__ (Self,career):
Self.career = career
def show_carrer (self):
Print Self.career
Ayumi = person ("diva") #创建一个对象时__init__ construction method is executed.
Ayumi.show_carrer ()
>>>diva
Then what is self?
The self in this case is the object created by itself. That is, the object that was created is not clear enough to say.
__name__ gets the name of the class.
__doc__: Gets the comment information for the class.
__BASE__: Gets the first parent class of this class.
__BASES__: Returns all the parent classes of the current class as tuples.
__DICT__: Returns a property of a class in a dictionary.
__MODULE__: Returns the module to which the current class belongs.
__class__: Gets the class to which an object belongs.
__del__: Deconstruction method executes the code under this method when an object is deleted or purged by the Python garbage collection mechanism. In the Python interpreter, an object to be reclaimed by the garbage collection mechanism or the object to be completely purged in memory must satisfy a condition that the object's reference count must be 0 when all references to the object are cleared, the reference count is 0 o'clock and the __del__ is triggered. method if you want to do something when the object is deleted, it can be implemented in the Deconstruction method.
Supplemental description only one destructor per object can be called
Here is an example of how the deconstruction method can be easily understood.
Class Num:
Count = 0
def __init__ (self):
Num.count + = 1
def __del__ (self):
Num.count-= 1
@classmethod
def show_count (CLS): Defines a class method so that the class can call the Show_count method directly to see the current return Num.count count value.
First, a num class is defined.
Print Num.show_count ()
>>>0
The value for count in the Num class is 0, and then we instantiate two objects with the Num class named A and B, respectively.
A = num ()
b = num ()
After instantiating two objects, call Num's class method show_count to see the value of the current count into a few
Print Num.show_count ()
>>>2
This does not need to do too much to explain the instantiation of two objects is equivalent to the implementation of the class two __init__ construction method.
Next, manually delete an object that we just created using the NUM class.
Del A
Print Num.show_count ()
>>>1
The Count property in the Num class is directly 1 from 2 to 1 which means the code under the __del__ destructor is executed.
Here's a little bit more to add. The principle of deleting an object is to set the reference count of an object to a 0 reference count once it is set to 0 then this object is reclaimed by the garbage collection mechanism inside python
4. What is the purpose of the object?
object is used to make a reference to the property.
Each object has its own data property. Python's class mechanism binds class-related functions to methods called objects on objects generated by this class.
#!/usr/bin/python2.7
#-*-Coding:utf-8-*-
Class Person:
def __init__ (Self,career):
Self.career = career
def show_carrer (self):
Print Self.career
Ayumi = person ("diva")
Ayumi.show_carrer ()
Like just a person this class instantiation of an object called Ayumi Show_carrer This method is the Ayumi object generated by the person class, which is the function defined in the class.
This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1912560
8.python Face Object Part.1 (first Class and object)