The difference between the new class classic class in Python (that is, whether the class inherits object)

Source: Internet
Author: User

First, what is the new class classic class?

# the new class refers to the class that inherits the object class A (obect):      ... .. # Classic class refers to a class that does not inherit object class A:      ......

Python recommends that you use the modern Class 1. New sure good ha, already compatible with the classic class

2. Fixed multiple inheritance bug in classic class

Let's focus on the multiple inheritance bug

BC is a subclass, D is a subclass of BC, there is a Save method in a, and C overrides it.

In the classic class, call D's Save method to search for the depth-first path, B-A-C, to execute the save in a is obviously unreasonable
The Save method in the call d of the new class is searched by the breadth-first path b-c-a, executed as C in save

(for depth first, breadth first puzzled dot here)

#Classic ClassclassA:def __init__(self):Print 'This is A'    defSave (self):Print 'come from A'classB (A):def __init__(self):Print 'This is B'classC (A):def __init__(self):Print 'This is C'    defSave (self):Print 'come from C'classD (b,c):def __init__(self):Print 'This is D'D1=D () d1.save ()#The result is ' come from A '
#New ClassclassA (object):def __init__(self):Print 'This is A'    defSave (self):Print 'come from A'classB (A):def __init__(self):Print 'This is B'classC (A):def __init__(self):Print 'This is C'    defSave (self):Print 'come from C'classD (b,c):def __init__(self):Print 'This is D'D1=D () d1.save ()#The result is ' come from C '

The difference between the new class classic class in Python (that is, whether the class inherits object)

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.