"Python" initializes the superclass with super

Source: Internet
Author: User

Initializes the traditional way of the superclass, calling the __init__ () method of the superclass in an instance of the subclass.

But the traditional approach has two problems, such as:

Question 1:

1 classMyBaseClass:2     def __init__(self, value):3Self.value =value4 5 6 classTimestwo:7     def __init__(self):8Self.value *= 29 Ten  One classplusfive: A     def __init__(self): -Self.value + = 5 -  the  - classOneWay (MyBaseClass, Timestwo, plusfive): -     def __init__(self,value): -MyBaseClass.__init__(self, value) +Timestwo.__init__(self) -Plusfive.__init__(self) +  A  at classAnotherway (MyBaseClass, Plusfive, timestwo): -     def __init__(self,value): -MyBaseClass.__init__(self, value) -Timestwo.__init__(self) -Plusfive.__init__(self) -  inFoo = OneWay (5) - Print('OneWay (5*2) +5=', Foo.value) toFoo = Anotherway (5) + Print('Anotherway:', Foo.value)

The result is:

As you can see from the results, the order of the calls does not change even if the inheritance order of the subclasses is changed.

Question 2:

If a subclass inherits from two separate superclass, and those two superclass inherit from the same common base class, then the diamond-type inheritance is formed.

This inheritance causes the common base class at the top of the diamond to execute the __init__ () method multiple times, resulting in an accident.

Like what:

1 classMyBaseClass:2     def __init__(self, value):3Self.value =value4 5 6 classtimesfive (mybaseclass):7     def __init__(self, value):8MyBaseClass.__init__(self, value)9Self.value *= 2Ten  One  A classPlustwo (mybaseclass): -     def __init__(self, value): -MyBaseClass.__init__(self, value) theSelf.value + = 2 -  -  - classThisway (timesfive, plustwo): +     def __init__(self, value): -Timesfive.__init__(self, value) +Plustwo.__init__(self, value) A  at  -  -Foo = Thisway (5) - Print('should be (5*5) +2', Foo.value)

When calling plustwo.__init__ (), call mybaseclass.__init__ again () again to change value to 5

Both of these problems can be solved using super, the method resolution order MRO, and the standard process to arrange the initialization order between superclass.

Like this one, using super to initialize Super class

1 classMyBaseClass:2     def __init__(self, value):3Self.value =value4 5 6 classtimesfive (mybaseclass):7     def __init__(self, value):8Super (timesfive, self).__init__(value)9Self.value *= 5Ten  One  A classPlustwo (mybaseclass): -     def __init__(self, value): -Super (Plustwo, self).__init__(value) theSelf.value + = 2 -  -  - classGoodway (timesfive, plustwo): +     def __init__(self, value): -Super (Goodway, self).__init__(value) +  A  atFoo = Goodway (5) - Print('should be 5* (5+2) and is', Foo.value)

and can be used

Print (GOODWAY.MRO ())

To view the order of initialization:

Reference: Effective Python

"Python" initializes the superclass with super

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.