super() = next in line

  1. Sometimes a class extends an existing method, but it wants to use the original implementation inside the redefinition. For this, use super():
  2. class Stock:
        ...
        def cost(self):
            return self.shares * self.price
        ...
    
    class MyStock(Stock):
        def cost(self):
            # Check the call to `super`
            actual_cost = super().cost()  # In Python 2: super(MyStock, self).cost()
            return 1.25 * actual_cost
  3. Ways to access parent class method. super().__init__() and Parent.__init__(self)
    1. class ChildA(Base):
          def __init__(self):
              Base.__init__(self)
      
      class ChildB(Base):
          def __init__(self):
              super().__init__()
      https://stackoverflow.com/questions/42413670/whats-the-difference-between-super-and-parent-class-name
    2. In general, they have the diff. when you use multiple inheritancesuper()delegates to the next object in the Method Resolution Order (MRO).
  4. super() with args if __init__() of parent class needs args
  5. class Stock:
        def __init__(self, name, shares, price):
            self.name = name
            self.shares = shares
            self.price = price
    
    class MyStock(Stock):
        def __init__(self, name, shares, price, factor):
            # Check the call to `super` and `__init__`
            super().__init__(name, shares, price)
            self.factor = factor
    
        def cost(self):
            return self.factor * super().cost()
  6. The order of executing __init__()
  7. C.__mro__ → (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.Base'>, <class 'object'>)
    C.__mro__ → (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.Base'>, <class 'object'>)
    super() with parameters
    super() with parameters
SuperMade with Super