classe - Comprendre Python super() avec les méthodes init()

Mots clés : pythonclassoopinheritancesuperpython

meilleur 3 Réponses classe - Comprendre Python super() avec les méthodes init()

vote vote

93

class ChildB(Base):     def __init__(self):         super().__init__() 
        super(ChildB, self).__init__() 
        Base.__init__(self) # Avoid this. 
class ChildA(Base):     def __init__(self):         Base.__init__(self)  class ChildB(Base):     def __init__(self):         super().__init__() 
class ChildB(Base):     def __init__(self):         mro = type(self).mro()         check_next = mro.index(ChildB) + 1 # next after *this* class.         while check_next < len(mro):             next_class = mro[check_next]             if '__init__' in next_class.__dict__:                 next_class.__init__(self)                 break             check_next += 1 
class ChildB(Base):     def __init__(self):         mro = type(self).mro()         for next_class in mro[mro.index(ChildB) + 1:]: # slice to end             if hasattr(next_class, '__init__'):                 next_class.__init__(self)                 break 
class Base(object):     def __init__(self):         print("Base init'ed")  class ChildA(Base):     def __init__(self):         print("ChildA init'ed")         Base.__init__(self)  class ChildB(Base):     def __init__(self):         print("ChildB init'ed")         super().__init__() 
class UserDependency(Base):     def __init__(self):         print("UserDependency init'ed")         super().__init__() 
class UserA(ChildA, UserDependency):     def __init__(self):         print("UserA init'ed")         super().__init__()  class UserB(ChildB, UserDependency):     def __init__(self):         print("UserB init'ed")         super().__init__() 
>>> UserA() UserA init'ed ChildA init'ed Base init'ed <__main__.UserA object at 0x0000000003403BA8> 
>>> UserB() UserB init'ed ChildB init'ed UserDependency init'ed Base init'ed <__main__.UserB object at 0x0000000003403438> 
super(self.__class__, self).__init__()  # DON'T DO THIS! EVER. 
>>> class Polygon(object): ...     def __init__(self, id): ...         self.id = id ... >>> class Rectangle(Polygon): ...     def __init__(self, id, width, height): ...         super(self.__class__, self).__init__(id) ...         self.shape = (width, height) ... >>> class Square(Rectangle): ...     pass ... >>> Square('a', 10, 10) Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "<stdin>", line 3, in __init__ TypeError: __init__() missing 2 required positional arguments: 'width' and 'height' 
vote vote

90

super().__init__() 
super(self.__class__, self).__init__()  # DON'T DO THIS! 
class Polygon(object):     def __init__(self, id):         self.id = id  class Rectangle(Polygon):     def __init__(self, id, width, height):         super(self.__class__, self).__init__(id)         self.shape = (width, height)  class Square(Rectangle):     pass 
vote vote

73

Base = ChildB  Base() 
Base = ChildA  Base() 

Questions similaires