What is the inheritance of Python classes? What are the rules for inheriting classes?

Source: Internet
Author: User
Let's look at the inheritance of Python classes in this article, and for a friend who has just come into contact with Python's programming language, there should be less knowledge of the Python class's inheritance, but it's okay, in the next article we'll look at inheritance rules for Python classes, I'll be in the following examples of inheritance for Python classesfor analysis.

Inheritance of Classes

One of the main benefits of object-oriented programming is the reuse of code, and one way to implement this reuse is through inheritance mechanisms.

A new class created through inheritance is called a subclass or derived class, and the inherited class is called a base class, parent class, or superclass.

Inheritance syntax

class derived classes name (base class name) ...    

Some of the characteristics of inheritance in Python:

1, if the constructor of the parent class is required in the subclass, the constructor method of calling the parent class needs to be displayed, or the constructor of the parent class is not overridden. Detailed description can be viewed: The Python subclass inherits the parent class constructor description.

2. When calling a method of a base class, you need to prefix the class name of the base class with the Self argument variable. The difference is that you do not need to take the self argument when calling a normal function in a class

3, Python always first looks for a method of the corresponding type, and if it cannot find the corresponding method in the derived class, it begins to look up one by one in the base class. (Find the method that was called in this class before you can find it in the base class).

If more than one class is listed in an inheritance tuple, it is called "Multiple inheritance."

Grammar

The declarations of derived classes, like their parent class, inherit the list of base classes followed by the class name, as follows:

Class Subclassname (parentclass1[, ParentClass2, ...]):    ...

Example analysis

#!/usr/bin/python#-*-coding:utf-8-*-class Parent:  # define parents class    parentattr =    def __init__ (self):        print "Call parent class constructor"    def Parentmethod (self):        print ' calls the parent class method '    def setAttr (self, attr):        parent.parentattr = attr    def getAttr (self):        print "Parent class Property:", Parent.parentattrclass Child (Parent):  # defines the subclass    def __init__ ( Self):        print "Call Subclass construction Method"    def Childmethod (self):        print ' call subclass method ' C = Child ()  # Instantiation Subclass C.childmethod ()  # Call the Subclass Method C.parentmethod ()  # Call the parent class method C.setattr ($)  # Call the parent class again-set the property value C.getattr ()  # Call the method of the parent class again-- Get property value

The result of the above code execution is as follows:

Call subclass method Call Subclass Methods Call Parent class method Parent Class Property: 200

You can inherit multiple classes

Class A:        # defines classes A.....class B:         # defines classes B.....class C (A, B):   # Inherit classes A and B .....

You can use the Issubclass () or Isinstance () method to detect.

1.issubclass ()-Boolean function to determine whether a class is a subclass of another class or descendant class, syntax: Issubclass (SUB,SUP)

2.isinstance (obj, Class) Boolean function returns True if OBJ is an instance object of class or is an instance object of class subclass.

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.