- https://www.youtube.com/watch?v=X1PQ7zzltz4
- https://dabeaz-course.github.io/practical-python/Notes/04_Classes_objects/02_Inheritance.html
- Sometimes a class extends an existing method, but it wants to use the original implementation inside the redefinition. For this, use
super(): - Ways to access parent class method.
super().__init__()andParent.__init__(self) - In general, they have the diff. when you use multiple inheritance,
super()delegates to the next object in the Method Resolution Order (MRO). - super() with args if __init__() of parent class needs args
- The order of executing __init__()
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_costclass ChildA(Base):
def __init__(self):
Base.__init__(self)
class ChildB(Base):
def __init__(self):
super().__init__()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()