Python Eighth Week Study notes (1)

Source: Internet
Author: User
Tags class definition

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
    • Class Cat (Animal) in parentheses for the parent class list

    • If the class definition does not have a parent class list, only the object class is inherited
    • The object class is the ancestor class of all classes
Special properties and methods of classes
    • Base
      • Base class for Class
    • 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
    • A class inherits from more than one class is multiple inheritance, and he will have characteristics of more than one class

    • Multiple inheritance is easy to cause ambiguity, so Python3 uses C3 algorithm to generate MRO ordered list to solve the two semantics of multiple inheritance (topological sort)
      https://kevinguo.me/2018/01/19/python-topological-sorting/#%E4%B8%80%E4%BB%80%E4%B9%88%E6%98%AF%E6%8B%93%E6%89 %91%e6%8e%92%e5%ba%8f
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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.