[Tutor] inheriting from a class

Christopher Spears cspears2002 at yahoo.com
Fri Sep 26 01:08:35 CEST 2008


I'm working out of "Game Programming (The L Line)" by Andy Harris.  He writes subclasses like so:

class TransCircle(collisionObjects.Circle):
    def __init__(self):
        collisionObjects.Circle.__init__(self)
        self.image.set_colorkey((255, 255, 255))

Basically, he is creating a TransCircle class that inherits attributes from a Circle class inside the collisionObjects module.  Is calling Circle's constructor inside of TransCircle's constructor necessary for inheritance to work?

Here is a little test that I ran:
In [2]: class A:
   ...:     def __init__(self):
   ...:         print "Class A!"
   ...:
   ...:

In [3]: class B (A):
   ...:     def __init__(self):
   ...:         print "Class B!"
   ...:
   ...:

In [4]:

In [4]: a = A()
Class A!

In [5]: b = B(a)
---------------------------------------------------------------------------
exceptions.TypeError                                 Traceback (most recent call last)

/media/<ipython console>

TypeError: __init__() takes exactly 1 argument (2 given)

In [6]: b = B()
Class B!

Looks like I don't have to call A's constructor.  Or am I missing something?



      


More information about the Tutor mailing list