0

I just learned about the inheritance in Python and how to use the super() method, as I understand its syntax works like this:

class Parent_class:
    def__init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2

class Subclass(Parent_class):
    def__init__(self, arg1, arg2, new_arg):
       super()__init__(arg1, arg2)
       self.new_arg= new_arg

But I just read a code where it comes in different structure:

class Subclass(Parent_class):
    def__init__(self, arg1, arg2, new_arg):
       super(Subclass, self).__init__()
       

Are these 2 ways similar? If not what is the different? Thank you all

  • This is the best and most complete explanation I know about about `super`: [mCoding: super](https://youtu.be/X1PQ7zzltz4). – Jorge Luis May 03 '23 at 11:35
  • 1
    You can consider `super()` as a update to the older `super(Subclass, self)` - to make it easier to write. You can replace any `super(Subclass, self).__init__(...)` with a `super().__init__()` – Glycerine May 03 '23 at 11:36
  • 1
    Also, might want to read [`super` documentation](https://docs.python.org/3/library/functions.html#super) – Jorge Luis May 03 '23 at 11:36
  • 1
    @Glycerine So basically there's no different Thanks, that's what I've expected. – sam Taghs May 03 '23 at 12:16
  • In short, experience showed that `super` was virtually never called with arguments other than the "current" class and `self`, so machinery was added in Python 3 to allow the compiler to infer those arguments when not provided. (They aren't true default parameter values, as the defaults depend on where `super` is called, not the definition of `super` itself.) – chepner May 03 '23 at 13:03
  • (Also, `super(SomeClass)` is distinct from `super(SomeClass, self)`, so supplying a default value for the second parameter would be wrong. Or alternatively, the default value for the second parameter would depend on whether a default was used for the first argument or not.) – chepner May 03 '23 at 13:06

0 Answers0