[Tutor] Dynamic variables names to objects of the same class ?

Alan Gauld alan.gauld at blueyonder.co.uk
Sat Aug 23 09:31:48 EDT 2003


> object willl have different properties in itself depending
> upon the keys it was initialized with.
> please lemme  know if theres any way this can be achieved in Python
?

There are ways of achieving this in Python using the getattr()
and setattr() methods for example. But I'd ask why you don't
want to use a dictionary? This looks like a classic dictionary
solution and using a class just for the sake of it doesn't make
sense. Is there something more that you want to do with the
class that requires that approach?

If you do need a class approach then why not just use a
dictionary inside the class? That will be more reliable and
faster than writing your own dictionary type object...

And if you are using Python 2.2 or above you could even try
making your class a subclass of the dictionary type.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list