Limitations of Python Advanced Cheng class 02:super

Source: Internet
Author: User

#-*-Coding:utf-8-*-

# python:2.x

__author__ = ' Administrator '

#当使用多重继承层次结构时, it is very dangerous to use super again, mainly because the class is initialized, and the base class is not implicitly called in the __init__.

#1滥用super和传统调用

#来自james Knight (http://funm.net/super-harmful) example, Class C calls its base class using the __init__ method, so that Class B is called 2 times

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 '

A.__init__ (self)

B.__init__ (self)

print ' MRO: ', [x.__name__ for X in c.__mro__] #MRO: [' C ', ' A ', ' B ', ' object ']

C ()

"""

C

A

B

B

------------

This occurs when the C instance calls a.__init__ (self), so super (a,self). __init__ () will call the B constructor, in other words, super should be even in the entire class hierarchy

The problem is that sometimes such hierarchies are part of the third-party code, and many of the related flaws that are introduced to the hierarchy by multiple inheritance can be found on the James page, and in order to avoid such problems, the problem should be

Before subclasses look at the __mro__ feature, if it doesn't exist, processing is a legacy class, and avoiding super might be safer

As follows:

"""

From Simplehttpserver import Simplehttprequesthandler

#print Simplehttprequesthandler.__mro__attributeerror:class Simplehttprequesthandler has no attribute ' __mro__ '

"""

If __mro__ exists, quickly look at the constructor code of the classes involved in each MRO, and if super is used everywhere, that's fine, or you can use it, or try to stay consistent.

Collections.deque can be safely sub-instantiated, it is possible to use super because it directly subclasses the object

"""

From collections Import Deque

Print deque.__mro__# (<type ' collections.deque ';, <type ' object ' >)

#randeom. Random is another class wrapper that exists in the _random module

From random import random

Print random.__mro__# (<class ' Random. Random ', <type ' _random. Random ';, <type ' object ' >)

#Zope类,

"""

From zope.app.container.brower.adding import adding as S

s.__mro__

Official: http://www.zope.com/

Interested friends can browse the next

"""

#不同种类参数

#super用法另一个问题是初始化中参数传递, how does a class invoke the __INIT__ code of its base class without the same signature?

Class Bases (object):

def __init__ (self):

print ' Bases '

Super (Bases,self). __init__ ()

Class B (Bases):

def __init__ (self):

print ' B '

Super (B,self). __init__ ()

Class B1 (Bases):

def __init__ (self):

print ' B1 '

Super (B1,self). __init__ ()

Class MyClass (B,B1):

def __init__ (Self,arg):

print ' My class arg '

Super (Myclass,self). __init__ (ARG)

#m =myclass (TypeError): __init__ () takes exactly 1 argument (2 given)

#解决方法之下是使用 *args,**kw Magic, all constructors pass all parameters, even if they are not used

Class Bb (object):

def __init__ (self,*args,**kw):

print ' B1 '

Super (Bb,self) __init__ (*args,**kw)

Class BB (BB):

def __init__ (self,*args,**kw):

print ' BB '

Super (Bb,self) __init__ (*args,**kw)

Class BBB (BB):

def __init__ (self,*args,**kw):

print ' BBB '

Super (Bbb,self) __init__ (*args,**kw)

Class MYCLSS (BB,BBB):

def __init__ (Self,arg):

print ' MYCLSS '

Super (Myclss,self). __init__ (ARG)

MM=MYCLSS (10)

"""

Myclss

Bb

Bbb

B1

"""

"""

But this is a bad fix because it causes all constructors to accept any type parameter, causing the code to become vulnerable, because any parameter is passed and passed, and the other solution is in YCLSS

Use the classic __init__ call, but this will result in the first defect

"""

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.