Python multiple inheritance Usage Python weekend study

Source: Internet
Author: User

python multiple inheritance Usage Python weekend study

inheritance is an important way of object-oriented programming, and through inheritance, subclasses can extend the functionality of the parent class. Python also has this feature, and in addition, Python can use multiple inheritance.

Grammar:

Class Subclass (BASE1,BASE2)

the meaning of this syntax is to create a Subclass class so that it inherits the same Base1 and the Base2 related attributes, and about inheritance also have a the following rules need to be followed:

1. Inheritance inherits only the methods of the parent class and cannot inherit the variables of the parent class;

2. To inherit the variables of the parent class, you need to execute the __init__ (self) method of the parent class;

3. The variable or method at the beginning of the underscore is considered to be protected and cannot be directly point out, but it can be used if forced to do so, but there is a warning;

4. You cannot use self in a static method, declare it as a static method with @staticmethod.

Instance:

Class A (object):

def __init__ (self):

Print (' A ')

Super (A, self). __init__ ()

Class B (object):

def __init__ (self):

Print (' B ')

Super (B, self). __init__ ()

class C (A, B):

def __init__ (self):

Print (' C ')

Super (C, self). __init__ ()

the above instance is a subclass C multiple inherits the characteristics of A and B, is the most basic usage of multiple inheritance, in addition, there are many uses, the use of multiple inheritance is to be aware of, improper use, it may bring more trouble than solve the problem, so unless the existing code to achieve the desired functionality, it is recommended not to consider using multiple inheritance!


Python multiple inheritance Usage Python weekend study

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.