0

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()
4
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by HiHe
Featured Replies
  • 2

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

  • [QUOTE=HiHe;1542585]This works with Python 2.7 [code]class B: def __init__(self, arg): print(arg) class C(B): def __init___(self, arg): super(C, self).__init__(arg) c = C('abc') # abc [/code]Here super() gives a TypeError: must be type, not classobj [code]from math import pi class Circle: """with Python3 object is inherited automagically""" def __init__(self, r): self.r = … Read More

  • 2

    Those multiple underscores are somewhat of a devil! :) As experts tonyjv and Griboullis pointed out, using [COLOR="Green"]Circle.__init__(self, r)[/COLOR] 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. Read More

2

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

Comments
thank you
good point about object inheritance
3

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) :)

Comments
I agree
thank you
2

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.

Comments
agreed on super() in python 3
thank you
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.