First, the concept of polymorphism:
In the real world there to understand that is to say, a thing in a variety of forms.
Such as water, solid, gas, liquid, etc.
In the world of the program is that multiple objects of different classes can be obtained by the same method, so that objects of different classes have different results.
It is not a special syntax, but a form.
Advantages: relative to the user, it largely reduces the difficulty of use. More user-friendly
Second, the multi-state implementations and case
Interfaces, abstract classes, duck type, are provided can write code for polymorphic, the simplest type of a duck
Case:
class Chicken: DEF Cry (Self): Print ( ' GGGG ' ) DEF the spawn (Self): Print ( ' the eggs ' ) class Duck (): DEF Cry (Self): Print ( ' Gaga Ga ' ) DEF the spawn (Self ): Print ( ' the duck ' ) J = Chicken () Y = Duck () DEFmany (obj): obj.spawn () volume (j) number (y)
Third, some of the functions and methods of application:
1,isinstance
This function is used to determine whether an object is an instance of a class
The first parameter is the object of judgment
The second parameter is a need to determine the type of
2,issubclass
This function is used to determine whether a class is a subclass of another class
The first parameter is a subclass of
The second parameter is the parent class
3,_ _str_ _
Str double the results when the object is converted to a string, the conversion is the return value of the function
Be used: it can be used to customize the layout object
4.Del
When you delete an object manually, it will immediately execute the function, or is automatically executed when the program ends
When the object is in use, the resources are not opened interpreter: usage scenarios.
Use Cases:
del class FileTool: def __init__(self,path): self.file = open(path,'rt',encoding = 'utf-8') self.a = 100 def read(self): return self.file.read() def __del__(self): self.file.close() tool = FileTool('a,txt')
5,call
When performed automatically when the calling object (the object is in parentheses)
6,slots
This property is a class attribute for the object optimizing memory usage
Optimized principle: the original is not a fixed number of attributes, becomes fixed. As a result, the interpreter does not create space for the name of the object, so as to achieve the effect of reducing memory overhead
Will lead to objects of this class can not add new attributes Once the slots in the class
7,getattr,setattr,delattr
getatter when you use point of access to property, if the property does not exist when it will perform
setatter When you use the point to set the property before execution
del delatter with the object when attributes. Will be performed
Fourth, the operator overloading
When we use a symbol, python interpreter will define a meaning for this symbol, while calling the corresponding handler
Originally custom object can not be used directly to compare the size of the operator, but we can customize the operator to achieve, also make custom objects support comparison operators