0

I am trying to understand classes in python 3, more specifically inheritance.

Let's consider the following example:

class Base():
    def __init__(self, arg1):
        self.arg1 = arg1
        
class Expansion(Base):
    def __init__():
        super().__init__(arg1)

class Extension(Base):
    def __init__():
        super(Extension, self).__init__(arg1)

Are the classes Expansion and Extension equivalent? If so, when does the argument of 'super' matter?

ty.
  • 221
  • 1
  • 8
  • If you define `__init__` in a subclass, it will override the parent class version of it. If you still want that code to run, you have to specifically call is. If you don't define `__init__` for the subclass, it will automatically execute the version from the parent class. – Aaron Jun 25 '20 at 17:52
  • 2
    They're equivalent. Python 2.x required the explicit parameters to `super()`, 3.x added some magic specifically to let `super()` determine the context itself. – jasonharper Jun 25 '20 at 17:52
  • 1
    Python 3 provides the ability use `super()` instead of `super(Extension, self)` because the compiler-determined defaults are virtually always the ones you want to use. You can pass a different first argument to bypass one or more classes in the MRO; I'm not actually aware of any practical use-case for passing a different object as the second argument. – chepner Jun 25 '20 at 17:56
  • 1
    Well, for example, you can use `super` outside of methods, or in a dynamically generated method added to a class later, in which case, you would have to use the two-argument for explicitly. For most practically purposes, the compiler magic is filling in the correct two-argument form you want. – juanpa.arrivillaga Jun 25 '20 at 17:56

0 Answers0