This works with Python 2.7

class B:
    def __init__(self, arg):
        print(arg)

class C(B):
    def __init___(self, arg):
        super(C, self).__init__(arg)

c = C('abc')  # abc

Here super() gives a TypeError: must be type, not classobj

from math import pi

class Circle:
    """with Python3 object is inherited automagically"""
    def __init__(self, r):
        self.r = r

    def area(self):
        """a method of class Circle, first arg is self"""
        area = pi * self.r**2
        return area


class Cone(Circle):
    """class Cone inherits class Circle"""
    def __init__(self, r, h):
        super(Cone, self).__init__(r)
        # Python27 gives TypeError: must be type, not classobj
        self.r = r
        self.h = h

    def volume(self):
        """a method of class Cone, first arg is self"""
        # notice how to call the method of class Circle
        vol = 1.0/3 * self.h * Circle.area(self)
        sf = "Cone of height = %s and radius = %s has volume = %0.2f"
        print(sf % (self.h, self.r, vol))


cone = Cone(r=4, h=10)
# methods of class Box are dot connected to the instance box
cone.volume()

So for Python2.7 you need Circle(object), which is clearer anyway.

commented: good point about object inheritance +14
commented: thank you +4

This works with Python 2.7

class B:
    def __init__(self, arg):
        print(arg)

class C(B):
    def __init___(self, arg):
        super(C, self).__init__(arg)

c = C('abc')  # abc

Here super() gives a TypeError: must be type, not classobj

from math import pi

class Circle:
    """with Python3 object is inherited automagically"""
    def __init__(self, r):
        self.r = r

    def area(self):
        """a method of class Circle, first arg is self"""
        area = pi * self.r**2
        return area


class Cone(Circle):
    """class Cone inherits class Circle"""
    def __init__(self, r, h):
        super(Cone, self).__init__(r)
        # Python27 gives TypeError: must be type, not classobj
        self.r = r
        self.h = h

    def volume(self):
        """a method of class Cone, first arg is self"""
        # notice how to call the method of class Circle
        vol = 1.0/3 * self.h * Circle.area(self)
        sf = "Cone of height = %s and radius = %s has volume = %0.2f"
        print(sf % (self.h, self.r, vol))


cone = Cone(r=4, h=10)
# methods of class Box are dot connected to the instance box
cone.volume()

In the "working" example, you wrote __init__ with 3 underscores on the right. It was probably not called. To me, super is useless. It's a purist's detail in the python language which only adds noise in your code. Better call Circle.__init__(self, r) :)

commented: thank you +4
commented: I agree +13

Those multiple underscores are somewhat of a devil! :)

As experts tonyjv and Griboullis pointed out, using Circle.__init__(self, r) is much clearer than using super().

The only advantage of super() is with Python3, where you can use it without hard-coding the class name.

commented: thank you +4
commented: agreed on super() in python 3 +13

Thank you all!
How foolish of me to forget the object inheritance with Python2, and to use three underscores. That was tough to see!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.