Python Learning (7) Object-oriented-class and instance, python object-oriented
Python object-oriented-classes and Instances
See Liao Xuefeng's Python tutorial in this chapter.
Class
Although Python is an explanatory language, it is object-oriented and can be used for object programming. As for what is object-oriented, we will not detail it here. Object-Oriented Programming itself is worth further learning. For more information, see other materials on the Internet.
The most important concepts of object-oriented are Class and Instance. Remember that classes are abstract templates, such as Student classes, instances are specific "objects" created based on classes. Each object has the same method, but its data may be different.
Taking the Student class as an example, in Python, the definition class isclass Keywords:
Note: For Python 2.X, you must add the pass Statement (object) next to the class name to indicate an empty statement segment.
class Followed by the class name, that isStudentThe class name is usually a word starting with an upper case, followed(object)Indicates the class from which the class is inherited. We will talk about the concept of inheritance later. Normally, if there is no suitable inheritance class, useobjectClass.
class Student(object): pass
Python 3.X requires only the class name and does not require (object)
class Student: pass
Instance
Instance creation is implemented by class name + () (if _ init _ does not or only self); If Student class is defined, you can create a Student instance based on the Student class, as shown below:
class Student: passMay = Student()print(May)
As you can see, the variable May points to a Student object. The 0x006C4770 is the memory address, and the address of each object is different. Student itself is a class.
You can freely bind attributes to an instance variable. For example, you can bind a name attribute to the instance "May", which is unique to the instance "May", and other newly created instances have no name attribute.
1 class Student: 2 pass3 May = Student () # create a May instance 4 print (May) 5 May. name = "May" # bind the name attribute to "May" 6 print (May. name) 7 Peter = Student () # create a Peter instance 8 # print (Peter. name) # An error is reported because Peter does not have the Name attribute.
So, if we need to bind an attribute to a class, how can we define it? See the following.
_ Init __
Because the class can act as a template, You can forcibly enter some attributes that we think must be bound when creating an instance. (Note _ init _ Double underline)
For Student classes, we define the attributes of name and score (all attributes required by Sudent) as follows:
class Student: def __init__(self, name, score): self.name = name self.score = score
The first parameter of the _ init _ method is always self, indicating the created instance itself. Therefore, you can bind various attributes to self within the _ init _ method, because self points to the created instance itself.
With the _ init _ method, you cannot input an empty parameter when creating an instance. You must input a parameter that matches the _ init _ method, but self does not need to be passed in. The Python interpreter will pass in the instance variable itself.