Inherited
- The basic concept of the individual inherited from the parents, inherited a part of the characteristics of parents, but also can have their own personality
- Subclasses inherit the parent class, have the properties and methods of the parent class, define their own properties, methods, and even override the properties and methods of the parent class.
Python Inheritance Implementation
Special properties and methods of classes
- Base
- Bases
- A tuple of the base class of a class
- Mro
- The lookup order of the class when the method resolves, returning the tuple
- MRO ()
- function Ibid., return list
- Subclasses ()
- Returns a list of subclasses of a class
Considerations in Python Inheritance
- belongs to a private member of the parent class, and the subclass is not directly accessible even if there is an inheritance relationship with the parent class (accessible through renamed property names, but with caution)
- Example:
class Animal: __count=100 heigtht=0 def showcount3(self): print(self.__count)class Cat(Animal): name=‘cat‘ __count=200c=Cat()c.showcount3()
Result is 100
Because the child class called the parent class to get the parent class private variable, the self. The count scope of count is under the parent class, which is actually called the Self._animal__count, and this property only has the parent class
- Workaround: Own private property, read and modify it in your own way, and do not use other methods of the class, even if it is a parent class or a method of a derived class
Property Lookup Order
- Example dict , class Dict , parent class Dict
Multiple inheritance
Mixin
- Nature is multiple inheritance
- Represents a combination of design patterns
Mixin Class Usage Principles
- init Initialization method should not appear in class
It is not usually possible to work independently because it is a partial feature implementation that is ready to be mixed into another class
Its ancestor class should also be the Mixin class
When using mixin, it is usually the first position in the inherited list
- Adorner implementation
def printable(cls): def _print(self): print(self.content, ‘decorator‘) cls.print = _print return clsclass Document: def __init__(self, content): self.content = contentclass Word(Document): passclass Pdf(Document): pass@printableclass PrintableWord(Word): passprint(PrintableWord.__dict__)print(PrintableWord.mro())pw = PrintableWord(‘test string‘)pw.print()@printableclass PrintablePdf(Word): pass
- Advantages:
- Simple and convenient, dynamically increases where needed, using adorners directly
Mixin implementation
class Document: def __init__(self, content): self.content = contentclass Word(Document): passclass Pdf(Document): passclass PrintableMixin: def print(self): print(self.content, ‘Mixin‘)class PrintableWord(PrintableMixin, Word): passprint(PrintableWord.__dict__)print(PrintableWord.mro())pw = PrintableWord(‘test string‘)pw.print()class SuperPrintableMixin(PrintableMixin): def print(self): print(‘~‘ * 20) super().print() print(‘~‘ * 20)class SuperPrintablePdf(SuperPrintableMixin, Pdf): passprint(SuperPrintablePdf.__dict__)print(SuperPrintablePdf.mro())spp = SuperPrintablePdf(‘super print pdf‘)spp.print()
Mixin class and Adorner
- Both of these ways can be used to see a person's preferences
- If you still need to inherit, you have to use the Mixin class
Python Eighth Week Study notes (1)