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

K Dhanvi dhanvik at gmx.net
Mon Aug 25 14:33:01 EDT 2003


Hi
I didn't want to use Dictionary since, then, the user would need to remember
the name of the dictionary each time he needs to access the keys inside it..

The configuration file has multiple key=value pairs that would be read and
validated when the configuration file is read.. For the final user of the
module it would be easier, if he can access the keys of the config file as
attributes of the configReader object rather than through a dictionary.this
would according to me simply accessing the keys for the user...this way he
need not know about the dictionary in which I would have to otherwise store
values.

The AddKey function would maintain a dictionary of the keys and their
datatypes for simple sanity validation of the input. This would help me to
remove some overhead from the end user on doing sanity checks on the values
for the config keys.

I would like to know whether there is any documentation on the web regarding
objects in Python and more functions like setattr and hasattr.. I dont think
these are so well documented functions on objects. So I am looking for help
from more experienced programmers on the group to show me a way :-).. Thanks
a lot for your help.

Dhanvi

PS : Sorry for the previous HTML posting.. I shld have checked my mail
format before sending.. :-)

----- Original Message ----- 
From: "Alan Gauld" <alan.gauld at blueyonder.co.uk>
To: "K Dhanvi" <dhanvik at gmx.net>; <tutor at python.org>
Sent: Saturday, August 23, 2003 1:01 PM
Subject: Re: [Tutor] Dynamic variables names to objects of the same class ?


> > 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>




More information about the Tutor mailing list