[Tutor] Creating subclasses (newbie)

Alan Gauld alan.gauld at blueyonder.co.uk
Thu Jun 24 03:30:38 EDT 2004

> I have a subclass I want to create - my intuition told me
> that it would be done like this:
> class MainClass:
> class SubClass:

While Python is an intuitive language learning by guesswork 
is probably not an effective method! :-)
Even if you stumble across something that seems to work its 
unlikely to be the best way. (Unless you are genetically 
related to Guido vannROssum maybe...)

You really should try following one of the many tutorials, 
the official one if you have already programmed another 
language or one of the others if you are a complete newbie.

The short answer to your question is:

class Main:
   # main definition code here

class Sub(Main):
   # sub extensions here

> subclassinstance = Sub()
> mainclassinstance = Main()

> But it seems that this isn't going to work. I'm reading a
> couple of Python books, but they don't seem to cover this
> topic very well (I don't see any coding examples).

Dunno which books you are reading but most Python tutorials 
should cover this topic, look under "inheritance" in the index.

Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list