When do I use super()?

Calling exactly super() is always redundant. It’s explicitly doing what would be implicitly done otherwise. That’s because if you omit a call to the super constructor, the no-argument super constructor will be invoked automatically anyway. Not to say that it’s bad style; some people like being explicit. However, where it becomes useful is when the … Read more

super() raises “TypeError: must be type, not classobj” for new-style class

Alright, it’s the usual “super() cannot be used with an old-style class”. However, the important point is that the correct test for “is this a new-style instance (i.e. object)?” is >>> class OldStyle: pass >>> instance = OldStyle() >>> issubclass(instance.__class__, object) False and not (as in the question): >>> isinstance(instance, object) True For classes, the … Read more

super() fails with error: TypeError “argument 1 must be type, not classobj” when parent does not inherit from object

Your problem is that class B is not declared as a “new-style” class. Change it like so: class B(object): and it will work. super() and all subclass/superclass stuff only works with new-style classes. I recommend you get in the habit of always typing that (object) on any class definition to make sure it is a … Read more

Is it unnecessary to put super() in constructor?

Firstly some terminology: No-args constructor: a constructor with no parameters; Accessible no-args constructor: a no-args constructor in the superclass visible to the subclass. That means it is either public or protected or, if both classes are in the same package, package access; and Default constructor: the public no-args constructor added by the compiler when there … Read more

super() in Java

super() calls the parent constructor with no arguments. It can be used also with arguments. I.e. super(argument1) and it will call the constructor that accepts 1 parameter of the type of argument1 (if exists). Also it can be used to call methods from the parent. I.e. super.aMethod() More info and tutorial here

Why is Python 3.x’s super() magic?

The new magic super() behaviour was added to avoid violating the D.R.Y. (Don’t Repeat Yourself) principle, see PEP 3135. Having to explicitly name the class by referencing it as a global is also prone to the same rebinding issues you discovered with super() itself: class Foo(Bar): def baz(self): return super(Foo, self).baz() + 42 Spam = … Read more

Calling parent class __init__ with multiple inheritance, what’s the right way?

The answer to your question depends on one very important aspect: Are your base classes designed for multiple inheritance? There are 3 different scenarios: The base classes are unrelated, standalone classes. If your base classes are separate entities that are capable of functioning independently and they don’t know each other, they’re not designed for multiple … Read more

What does ‘super’ do in Python? – difference between super().__init__() and explicit superclass __init__()

What’s the difference? SomeBaseClass.__init__(self) means to call SomeBaseClass‘s __init__. while super().__init__() means to call a bound __init__ from the parent class that follows SomeBaseClass‘s child class (the one that defines this method) in the instance’s Method Resolution Order (MRO). If the instance is a subclass of this child class, there may be a different parent … Read more