[Tutor] designing a class

Alan Gauld alan.gauld at freenet.co.uk
Fri Jan 27 09:14:38 CET 2006

> Write a class called Mylist that shadows ("wraps") a
> Python list: it should overload most list operators
> ...
> When I read this, I feel like a deer caught in the
> headlights.  Where should I begin?  How do I go about
> designing a new class?

Well, to quote an old joke, I wouldn't start from here....

Defining a new type based on a builtin type and overloading 
all the operators is a relatively hard thing to do (correctly). 
Personally I'd advise defining something easier and less useful.
Something that doesn't require operators at all initially just 
plain methods.

Once you've defined a few of those you will find the exercise
above more straightforward. Try wrioting a program that you 
are interested in that uses objects - like say a DVD database, 
or a menu management system. Think about how you might 
build DVD or menu classes. What kind of things would you 
do to these objects if you had them(their methods). What kind 
of information would those methods need to work?

Build the objects method by method and test them.

Just some ideas.

The one advantage of starting with a builtin type is that most of 
your code just calls the existing class's methods but I still think 
the advantage of that is outweighed by the added complexity
of jumping straight into overloading operators!


Alan G
Author of the learn to program web tutor

More information about the Tutor mailing list