Python---Object-oriented programming-2

Source: Internet
Author: User

. Classic class. New class. Depth first. Breadth First

First, the classic class is a noun in Python 2.x, Python 2.x default is the classic class, only the display inherits the object is the new class; in python3.x, the new class is the default.

Classic class explanation

Object is the "mother of all Classes". And in python2.x to write the object is the new class, so obviously nothing is written is the classic class, in fact, if you do not specify a parent class, or if the subclass of the basic class does not have a parent class, you create a classic class. Let's look at an example:

1 # !/usr/bin/python2 2 # -*-coding:utf-8-*- 3 4 class Myobjecttype: 5     ' define Myobjecttype Classic class ' 6     Class_suite
View CodeA new type of explanation

From the above, the class inherits the base class of object, which is the new class. However, in python3.x, the default is the new class, and the classic class is removed without having to display the inherited object.

1 #!/usr/bin/python32 #-*-coding:utf-8-*-3 4 classMyobjecttype (object):5     'define Myobjecttype New class'6 Class_suite7     8 classMynewobjecttype ():9     'define Myobjecttype New class'TenClass_suite
View Code

When it comes to classical and modern classes, it is important to mention the two nouns of depth first and breadth first.

The new-type MRO algorithm uses C3 algorithm breadth-first search, while the classic-class MRO algorithm uses depth-first search.

Depth first
1 #!/usr/bin/python22 #-*-coding:utf-8-*-3 4 classA:5     Pass6 classB:7     Pass8 classC (B):9     PassTen classD (c,a): One     Pass
View Code

The order of execution is: D->c->b,->a

Breadth First
1 #!/usr/bin/python32 #-*-coding:utf-8-*-3 4 classA (object):5     Pass6 classB (object):7     Pass8 classC (object):9     PassTen classD (a,b,c): One     Pass
View Code

The order of execution is: D->a->b->c->object

Python---Object-oriented programming-2

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.