I want to familiarize myself with the third subclass of the compiling class of Python.

Source: Internet
Author: User

I want to familiarize myself with the third subclass of the compiling class of Python.

As for the class, you may already feel it. Read the following code carefully and see if any problems can be found?
Copy codeThe Code is as follows:
#! /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 above code is running without any problems. However, after careful consideration, we found that there are two classes, one called Person and the other called Programmer. This is not the problem, the problem is that the constructor of these two classes has the same place: self. name = name, self. lang = lang, self. email = email, which is generally not allowed by programmers pursuing code quality. It is best not to have duplicate or redundant code. However, what should we do if these parameters are required in both classes?

Subclass, parent class, and inheritance

See the following code. There are two classes A and B. This program runs correctly. The function of each class is to print only the specified content.
Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Coding: UTF-8

Class:
Def _ init _ (self ):
Print "aaa"

Class B:
Def _ init _ (self ):
Print "bbb"

If _ name __= = "_ main __":
A = ()
B = B ()

# Running result
Aaa
Bbb

The two classes have no parent-child relationship. Change the Class B slightly, and observe the difference with the above.
Copy codeThe Code is as follows :#! /Usr/bin/env python
# Coding: UTF-8

Class:
Def _ init _ (self ):
Print "aaa"

Class B (A): # This is different from the above program. B inherits
Def _ init _ (self ):
Print "bbb"

If _ name __= = "_ main __":
A = ()
B = B ()

# Running result
Aaa
Bbb

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

However, the viewer finds no and the running result is the same. Yes, I thought that although B inherited A, he didn't call anything about A, just like his son inherited wealth from his father, but he didn't do anything, what the outside world sees is the same as what it does not inherit.
Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Coding: UTF-8

Class:
Def _ init _ (self ):
Print "aaa"

Class B ():
Def _ init _ (self ):
# Print "bbb"
A. _ init _ (self) # Run the inherited parent class

If _ name __= = "_ main __":
A = ()
B = B ()

# Running result
Aaa
Aaa

The running result has changed. B = B () is A Class B, but B inherits A and introduces the constructor A in the initialized constructor, run the result of.

In the following example, the end program at the beginning is rewritten as a subclass inheritance, which can be like this:
Copy codeThe Code is as follows:
#! /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) # inherit the functions of Person. _ init _ () here
# Self. name = name # the three statements have been fixed in Person, so there is no need to repeat them.
# Self. lang = lang # The functions of these three sentences have been implemented through inheritance.
# Self. email = email
Self. system = system # The Child class is different from the parent class of Person.
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 running result is the same as the previous one.

Whether the column position understands the characteristics of subclass, parent class, and inheritance. If you have a father who is a senior official or rich man, then you will be the second generation or the second generation. You will inherit a lot of wealth from them, so you don't have to be too tired in your life. This is the role of inheritance. Similar to code, inheritance can make code writing less effort.

For the reason why inheritance should be used, the friend @ Ling Huo Daxia gave a wonderful explanation:

Copy codeThe Code is as follows:
Technically speaking, the main purpose of inheritance in OOP is to implement multi-state. For polymorphism, it is important that the interface is inherited, and whether the attributes and behaviors are inherited is not necessarily. In fact, a large number of engineering practices show that heavy behavior inheritance will lead to excessive complexity and bloated systems, but will reduce flexibility. Therefore, we advocate the concept of mild inheritance based on interfaces. In this model, because the parent class (Interface Class) has no code, there is no code reuse at all.

In Python, because Duck Type exists, the importance of interface definition is greatly reduced, and the inheritance function is further weakened.

In addition, logically, the purpose of inheritance is not to reuse code, but to streamline the relationship.
 
I fully agree with the above explanation. However, it doesn't matter if you don't understand it. The spirit of the above explanation does need to be realized in programming practice.




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.