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