zoukankan      html  css  js  c++  java
  • python super()

    class Base(object):
        pass
    
    class Inject(Base):
        def __init__(self):
            super().__init__()                  # python3
            # super(Inject, self).__init__()    # python2
            pass
    
    class Child(Base):                  
        def __init__(self, user):
            super().__init__()                  # 1 python3 super() lets you avoid referring to the base class explicitly
            # super(Child, self).__init()       # 2 python2
            # Base.__init__(self)               # 3 avoid this, it limit you use multiple inheritance
            self.user = user
    
    class SuperInjector(Child, Inject):
        pass
        # if Child() use SuperInjector() # 2  can call Inject()
        # if Child() SuperInjector() use # 3 can't call Inject()
    
    
    
    # conclusion:Always use super to reference the parent class !!!
    
    # super() issue. In a class hierarchy with single inheritance, super() can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable; support cooperative multiple inheritance in a dynamic execution environment.   addr:https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods
    # standard doc on super. difference between 1 and 2.    addr:https://stackoverflow.com/questions/222877/what-does-super-do-in-python
    # method resolution order(MRO)   addr:https://stackoverflow.com/questions/2010692/what-does-mro-do
    # difference between 2 and 3.   addr:https://stackoverflow.com/questions/222877/what-does-super-do-in-python/33469090#33469090
  • 相关阅读:
    链表
    Wonder团队承接各种Web3D业务
    真我的信息
    一个人独自做长期项目,如何提高工作效率?
    【Java】类的结构
    【长知识】找书攻略
    【长知识】语义化版本控制
    【Java】Debug调试常用技巧
    【Web】Servlet三大作用域、JSP四大作用域
    【Web】Servlet基本概念
  • 原文地址:https://www.cnblogs.com/vickey-wu/p/7908601.html
Copyright © 2011-2022 走看看