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

I get some error that I can’t figure out. Any clue what is wrong with my sample code? class B: def meth(self, arg): print arg class C(B): def meth(self, arg): super(C, self).meth(arg) print C().meth(1) I got the sample test code from help of ‘super’ built-in method. Here is the error: Traceback (most recent call last): … Read more

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

Say I have a multiple inheritance scenario: class A(object): # code for A here class B(object): # code for B here class C(A, B): def __init__(self): # What’s the right code to write here to ensure # A.__init__ and B.__init__ get called? There’s two typical approaches to writing C‘s __init__: (old-style) ParentClass.__init__(self) (newer-style) super(DerivedClass, self).__init__() … Read more

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

The following use of super() raises a TypeError: why? >>> from HTMLParser import HTMLParser >>> class TextParser(HTMLParser): … def __init__(self): … super(TextParser, self).__init__() … self.all_data = [] … >>> TextParser() (…) TypeError: must be type, not classobj There is a similar question on StackOverflow: Python super() raises TypeError, where the error is explained by the … Read more

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

What’s the difference between: class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() and: class Child(SomeBaseClass): def __init__(self): SomeBaseClass.__init__(self) I’ve seen super being used quite a lot in classes with only single inheritance. I can see why you’d use it in multiple inheritance but am unclear as to what the advantages are of using it in this kind … Read more

What is PECS (Producer Extends Consumer Super)?

I came across PECS (short for Producer extends and Consumer super) while reading up on generics. Can someone explain to me how to use PECS to resolve confusion between extends and super? 16 s 16 tl;dr: “PECS” is from the collection’s point of view. If you are only pulling items from a generic collection, it … Read more

Understanding Python super() with __init__() methods [duplicate]

This question already has answers here: What does ‘super’ do in Python? – difference between super().__init__() and explicit superclass __init__() (11 answers) Closed 6 years ago. Why is super() used? Is there a difference between using Base.__init__ and super().__init__? class Base(object): def __init__(self): print “Base created” class ChildA(Base): def __init__(self): Base.__init__(self) class ChildB(Base): def __init__(self): … Read more