▲__slots__
After defining a class and creating an instance of class, we can bind any property and method to that instance. This is the flexibility of dynamic language.
The instance code is as follows:
#defines a class of student>>>classStudent (object): ...Pass...#create an instance of the class student STU1>>> STU1 =Student ()#binding an attribute to an instance name>>> Stu1.name ='Michael'>>>Stu1.namemichael#binding an attribute to an instance age>>> Stu1.age =' -'>>>Stu1.age25#defining the Set_age () function as an instance method>>>defSet_age (self, age): ... self.age=Age ...#Import Methodtype Module>>> fromTypesImportMethodtype#Stu1 Binding method to Instance Set_age ()>>> Stu1.set_age =Methodtype (Set_age, STU1)#Invoking instance methods>>> Stu1.set_age (20)#Test Results>>>Stu1.age20#create another instance of class student Stu2>>> STU2 =Student ()#tests the properties that are bound to STU1 name and Method Set_age () are available for STU2>>>Stu2.nametraceback (most recent): File"<stdin>", Line 1,inch<module>Attributeerror:'Student'object has no attribute'name'>>> Stu2.set_age (22) Traceback (most recent): File"<stdin>", Line 1,inch<module>Attributeerror:'Student'object has no attribute'Set_age'#create a class method and bind to the class student>>>defSet_score (self, score): ... self.score=score ...>>> Student.set_score =Methodtype (Set_score, Student)#test the method of binding class student Set_score () is available for STU1 and STU2>>> Stu1.set_score (98)>>>Stu1.score98>>> Stu2.set_score (70)>>>Stu2.score70
In fact, the above code Set_score () can be directly defined in class student, but dynamic binding also allows us to dynamically add functionality to the program as it runs.
If we want to limit the dynamically added properties, we can define class again by defining a special __slots__ variable to limit the attributes that the class can add.
The instance code is as follows:
>>>classStudent (object):#Define a property name that allows binding by using a tuple...__slots__= ('name',' Age')...>>> STU3 =Student ()>>> Stu3.name ='Jack'>>> Stu3.age =' A'#test for unbound properties score can add>>> Stu3.score =' the'Traceback (most recent): File"<stdin>", Line 1,inch<module>Attributeerror:'Student'object has no attribute'score'#Test __slots__ If the child class is working>>>classFirstgrade (Student): ...Pass...>>> Stu4 =Firstgrade ()>>> Stu4.name ='David'>>> Stu4.age =' A'>>> Stu4.score =' -'
▲property Decorator
Official document Address link: https://docs.python.org/3/library/functions.html?highlight=property#property
@property can turn a method into a property call. Put a getter method programming attribute, just add @property. @*.setter is responsible for turning a setter method into a property assignment.
The sample code is as follows:
classStudent (object):def __init__(self): Self._score=None#Add getter for attribute score@propertydefscore (self):Print('You get the score!') returnSelf._score#increase setter for attribute score@score. Setterdefscore (self, value):Print('You set the score!') Self._score=value#add deleter for attribute score@score. Deleterdefscore (self):Print('You delete the score!') delSELF._SCORESTU1=Student ()Print('-'* 50) Stu1.score= 88Print('-'* 50)Print(Stu1.score)Print('-'* 50) Stu1.score= 92Print(Stu1.score)delStu1.score
Execution Result:
--------------------------------------------------youset the score! --------------------------------------------------youget the score! --------------------------------------------------youset the score!you get the score! Delete the score!
▲ Multiple Inheritance
When you design an inheritance relationship for a class, the main line is usually a single inheritance. If additional functionality needs to be added, multiple inheritance can be implemented. This design is often called mixin.
The sample code is as follows:
class Animal (object): Pass class flyable (object): def Fly (self): Print ('flying ... ' )# defines a subclass of multiple inheritance class Eagle (Animal, flyable): Pass
Python note 6# object-oriented advanced programming one