< class definitions and object declarations >
The most important concept of object-oriented is class and instance (Instance), it must be kept in mind that classes are abstract templates , such as student classes, and instances are specific "objects" created from classes, Each object has the same method, but the individual data may be different.
? class definition
Take the student class as an example, in Python, the definition class is through the Class keyword:
class Student(object): pass
followed by class name, student, class name is usually the beginning of the word in uppercase , followed by (object), indicating which class is inherited from, usually, if there is no appropriate inheritance class, use the The object class, which is the class that all classes will eventually inherit (and Java is a bit like).
? Create an Object
it's all defined. Student class, you can base Student class creates a Student instance, the creation of an instance is achieved by "class name + ()"(there is no plus sign in the actual application):
>>> bart = Student()>>> bart<__main__.Student object at 0x10a67a590>>>> Student<class ‘__main__.Student‘>
As you can see, the variable Bart is pointing to an instance of student, and the latter 0x10a67a590 is the memory address, each object has a different address, and student itself is a class. you are free to bind attributes to an instance variable, for example, Bart binds a Name property:
>>> bart.name = ‘Bart Simpson‘>>> bart.name‘Bart Simpson‘
Because a class can act as a template, you can force a number of attributes that we think must be bound to be filled in when creating an instance. By defining a special __init__ method, when creating an instance, bind the attributes such as Name,score :
class Student(object): def __init__(self, name, score): self.name = name self.score = score
?Note:· Special method "Init" has
two underscores before and after! Note that the first argument of the _
_init__ method
is alwaysself, which represents the created instance itself, so within the __init__ method, you can bind various properties to
the Self points to the created instance itself . with the __init__ method, when you create an instance, you cannot pass in an empty argument, you must pass in a parameter that matches the __init__ method, but self does not need to be passed, and the Python interpreter will pass the instance variable in itself:
>>> bart = Student(‘Bart Simpson‘, 59)>>> bart.name‘Bart Simpson‘>>> bart.score59
A function defined in a class is only a bit different than a normal function , which is that the first argument is always the instance variableself and, when called, does not pass the argument. In addition, there is no difference between a class method and a normal function, so you can still use default parameters, variable arguments, keyword arguments, and named keyword parameters.
< data encapsulation >
An important feature of object-oriented programming is data encapsulation. In the student class above, each instance has its own name and score the data. This data can be accessed through functions, such as printing a student's score:
>>> def print_score(std):... print(‘%s: %s‘ % (std.name, std.score))...>>> print_score(bart)Bart Simpson: 59
However, since the student instance itself has this data, it is not necessary to access the data from outside the function to access, you can directly within the student class to define the function of accessing the data , so that the "data" to Packaged up . The functions of these encapsulated data are associated with the student class itself, and we call it the method of the class :
class Student(object): def __init__(self, name, score): self.name = name self.score = score def print_score(self): print(‘%s: %s‘ % (self.name, self.score))
To define a method, the other one is the same as the normal function except that the first argument is self. To invoke a method, just call it directly on the instance variable, except that self does not pass, and other parameters are passed in normally:
>>> bart.print_score()Bart Simpson: 59
In this way, we look at the student class from the outside, just need to know that the creation of the instance needs to give name and score, and how to print, is defined within the student class, the data and logic is "encapsulated" , the call is easy, But without knowing the details of the internal implementation. Another good place to package is to add new methods to the student class, such as Get_grade:
class Student(object): ... def get_grade(self): if self.score >= 90: return ‘A‘ elif self.score >= 60: return ‘B‘ else: return ‘C‘
Similarly, the Get_grade method can be called directly on an instance variable without needing to know the internal implementation details:
>>> bart.get_grade()‘C‘
< summary >
A class is a template that creates an instance, and an instance is a concrete object, each instance has data that is independent from each other, and the method is a function that is bound to an instance, unlike a normal function, which accesses the data of the instance directly, and by invoking the method on the instance, We directly manipulate the data inside the object, but do not need to know the implementation details inside the method. Unlike static languages, Python allows you to bind any data to an instance variable, which means that for two instance variables, although they are different instances of the same class, the variable names you have may be different:
>>> bart = Student(‘Bart Simpson‘, 59)>>> lisa = Student(‘Lisa Simpson‘, 87)>>> bart.age = 8>>> bart.age8>>> lisa.ageTraceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: ‘Student‘ object has no attribute ‘age‘
<wiz_tmp_tag id= "Wiz-table-range-border" contenteditable= "false" style= "Display:none;" >
From for notes (Wiz)
Python Object-oriented programming-class definitions and objects