[Tutor] Eureka!

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Feb 29 19:59:56 EST 2004


> I copied the UserDict module and created a module
> called UserDict2.  Inside this module, I created the
> following class and definition:
> 
> class ODict(UserDict):

Why did you copy the UserDict module?

You don't need to copy the module to access the class, 
just import it.

> their position in the list.  I do have a question
> though.  In the past, when I entered something like:
> 
> class ODict(UserDict):
>     order = []
>     def __setitem__(self, key, item):
> 
> I would get some sort of error about the order
> variable.  Can anyone explain this?  I wish I remember
> what I wrote, but it's all a big blur to me.  Should I
> have used __init__ to declare the order variable? 
> What is the purpose of __init__ anyway?

If you want each instance of your class to have its own order 
- and presumably you do! - then you need to set order, or more 
precisely self.order within the __init__ method. Otherwise all 
instances of your class will share the same order list.

The purpose of init is to initialise the variables of a new 
object instance. Any variables you set up inside init will 
be unique to that instance.

HTH,

Alan G



More information about the Tutor mailing list