Learn three subclasses of Python's authoring classes

  • 2020-04-02 14:14:08
  • OfStack

Please read the following code carefully and see if you can find any problems.


#!/usr/bin/env python
#coding:utf-8 class Person:
    def __init__(self, name, lang, email):
        self.name = name
        self.lang = lang
        self.email = email     def author(self):
        return self.name class Programmer:
    def __init__(self, name, lang, email, system, website):
        self.name = name
        self.lang = lang
        self.email = email
        self.system = system
        self.website = website     def pythoner(self):
        pythoner_list = [ self.name, self.lang, self.email, self.system, self.website ]
        return pythoner_list if __name__=="__main__":
    writer = Person("qiwsir","Chinese","qiwsir@gmail.com")
    python = Programmer("qiwsir","Python","qiwsir@gmail.com","Ubutun","qiwsir.github.io")
    print "My name is:%s"%writer.author()
    print "I write program by:%s"%python.pythoner()[1]

  The code above, to run is no problem, but, look closely, found that there are two classes, a named Person, also called a Programmer, this is not the problem, the problem is the constructor of these two classes, where there is the same: the self. The name = name, self. Lang = lang, self. Email = email, this for programmers in pursuit of code quality, generally is not allowed. It is best not to have duplicate or redundant code. But what if you have these parameters in both classes?

Subclasses, superclasses, and inheritance

If you look at the code below, there are two classes A and B. The program runs correctly, and each class simply prints the specified content.


#!/usr/bin/env python
#coding:utf-8 class A:
    def __init__(self):
        print "aaa" class B:
    def __init__(self):
        print "bbb" if __name__=="__main__":
    a = A()
    b = B() # The results
aaa
bbb

  The two classes above have no so-called parent-child relationship with each other. Now change it a little bit, rewrite class B, and notice the difference.

#!/usr/bin/env python
#coding:utf-8 class A:
    def __init__(self):
        print "aaa" class B(A):         # This is different from the above program. B inherited A
    def __init__(self):
        print "bbb" if __name__=="__main__":
    a = A()
    b = B() # The results
aaa
bbb

  In this program, class B is A little different from the previous one, class B(A):, which indicates the relationship between B and A: B is A subclass of A, and B inherits everything from A.

However, if the viewer finds out, the result is the same. Yes, that's because you don't call anything about A in B even though you inherit A, just like A son inherits wealth from his father, but the son doesn't move A single child, the outside world sees the same thing as no inheritance.


#!/usr/bin/env python
#coding:utf-8 class A:
    def __init__(self):
        print "aaa" class B(A):
    def __init__(self):
        #print "bbb"
        A.__init__(self)    # Run the inherited parent class if __name__=="__main__":
    a = A()
    b = B() # The results
aaa
aaa

  This time the result is changed, b= b () is the running class b, but b inherits A, and in the initializing constructor, the constructor of A is introduced, so the result of running A is the corresponding result.

Now let's subclass the program at the beginning. It could look like this:


#!/usr/bin/env python
#coding:utf-8 class Person:
    def __init__(self, name, lang, email):
        self.name = name
        self.lang = lang
        self.email = email     def author(self):
        return self.name
"""
class Programmer:
    def __init__(self, name, lang, email, system, website):
        self.name = name
        self.lang = lang
        self.email = email
        self.system = system
        self.website = website     def pythoner(self):
        pythoner_list = [ self.name, self.lang, self.email, self.system, self.website ]
        return pythoner_list
""" class Programmer(Person):       # Inherit the parent class Person
    def __init__(self, name, lang, email, system, website):
        Person.__init__(self,name,lang,email)   # will Person.__init__() Is inherited here
        #self.name = name                       # This is three sentences Person You don't have to repeat what you've already done
        #self.lang = lang                       # These three sentences have been implemented by inheritance
        #self.email = email
        self.system = system                    # That's different in subclasses Person The parent class part
        self.website = website     def pythoner(self):
        pythoner_list = [ self.name, self.lang, self.email, self.system, self.website ]
        return pythoner_list if __name__=="__main__":
    writer = Person("qiwsir","Chinese","qiwsir@gmail.com")
    python = Programmer("qiwsir","Python","qiwsir@gmail.com","Ubutun","qiwsir.github.io")
    print "My name is:%s"%writer.author()
    print "I write program by:%s"%python.pythoner()[1]

  The code runs the same as before.

Does the column understand the characteristics of subclasses and superclasses and inheritance? If you have a father who is a senior official or a rich person, then you are an official or a rich person, and you inherit a lot of wealth from them, so you don't have to work too hard. That's what inheritance does. In code, too, inheritance makes writing code less tedious.

Friend @linfoxbug has a great explanation for why inheritance is needed:


Technically, OOP The primary use of inheritance is to implement more State. For polymorphism, what matters is interface inheritance, whether properties and behaviors are inherited or not, which is not certain. In fact, a lot of engineering practice shows that heavy behavioral inheritance can lead to overly complex and bloated systems, It reduces flexibility. Therefore, the idea of light inheritance based on interface is now advocated. In this model there is no code at all for the parent class (the interface class), so there is no code reuse at all.
in Python Because it exists Duck Type The importance of interface definition has been greatly reduced and the role of inheritance has been further weakened.
In addition, logically speaking, the purpose of inheritance is not to reuse code, but to straighten out relationships.
 
I fully agree with the above explanation. However, if you do not understand, it does not matter, the spirit of the above explanation, really need to be in the practice of programming comprehension to understand.


Related articles: