2

Using python 2.7 with new class style, if my class inherits from Objectclass, what is the behavior of super(ClassName, self).__init__() ? What I mean is, what happens behind the scenes ? What is the difference if I omitted it ?

An example above:

  class ClassName(object):
    """docstring for ClassName"""
    def __init__(self, arg):
        super(ClassName, self).__init__() ## The question above is about this super
        self.arg = arg


  class OtherClass(ClassName):
    """docstring for OtherClass"""
    def __init__(self, arg, otherArg):
        super(OtherClass, self).__init__(arg) ## The question below is about this super
        self.otherArg = otherArg

If I omit the super, what is the happens behind the scenes ?

Thanks.

ViniciusArruda
  • 970
  • 12
  • 27
  • Note that the call to `super` in `OtherClass` is not safe, since there is no guaranteed that the next class in the MRO allows an argument to `__init__` (e.g., since it might be `object`). – chepner Apr 03 '16 at 13:53

1 Answers1

1

In the case of single inheritance, absolutely nothing. object.__init__() does squat.

In the case of multiple inheritance, one entire chain of initialization is not called. What this does to your application varies, but it's generally not a good thing to happen.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358