Method parsing Order (MRO) for Python

Source: Internet
Author: User

MRO is the method resolution order, which is used primarily to determine the path of a property that is tuned at multiple inheritance (from which class).

http://blog.csdn.net/imzoer/article/details/8737642

Do you really understand the MRO algorithm in Python?

Api:

1 classtype (object):2     """3 type (object), the object ' s type4 type (name, bases, Dict), a new type5     """6     defMRO (self):#real signature unknown; restored from __doc__7         """8 MRO () List9 return a type ' s method resolution orderTen         """ One         return[]

Test code:

1 #!/usr/bin/env python2 classA (object):3     def __init__(self):4         Print "Enter A"5         Print("Leave A")6 7 8 classB (object):9     def __init__(self):Ten         Print "Enter B" One         Print "Leave B" A  -  - classC (A, b): the     def __init__(self): -         Print "Enter C" -Super (C, self).__init__() -         Print "Leave C" +  -b =C () +Mo = B.__class__. MRO () A PrintMo

Output:

Enter C
Enter A
Leave A
Leave C
[<class ' __main__. C ';, <class ' __main__. A ';, <class ' __main__. B ';, <type ' object ';]

Method parsing Order (MRO) for Python

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.