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

Lloyd Kvam pythontutor at venix.com
Sat Aug 23 14:00:28 EDT 2003


The python function setattr should do what you want.  I expect you
would write something like:

class ConfigReader:
	# stuff
	def ReadConfigFile(self):
		# stuff to get key_names and key_value pairs so that
		# key_name = "MAX_THREADS and key_value = some_integer
		setattr( self, key_name, key_value)

Then your line:
 >         noOfThreads = config.MAX_THREADS
should work.

This example code does not make any use of whatever might have been
done in the AddKey method.

HTH


K Dhanvi wrote:

> Dear friends,
> 
>  I have a query about giving names to variables inside classes and thus to
> the objects of the classes..
> 
>  I have a class called configReader which reads a configuration file and
> then puts the configuration data into a dictionary at prsent. Before reading
> the configuration file, the config object is told what keys to expect and
> their datatpyes for simple santiy checks on the corresonding values.. At
> present the key and values are stored in a dictionary.. I would like to make
> each key a property of the respective configReader object.
>   for example => 
>  If one of my keys in the configuration file is MAX_THREADS..
> I would like my code to be as follows
> ------------------------------------------------------------------------------------- 
> 
>         import ConfigReader
>         config = ConfigReader( "./config.file")
>         config.AddKey( "MAX_THREADS", INT )
>         config.ReadConfigFile()
> 
>         noOfThreads = config.MAX_THREADS
> -------------------------------------------------------------------------------------
> 
> The first 3 lines of the sample code have been completed.. I would like to
> add the functionality similar to the final line of the sample code. Each
> object willl have different properties in itself depending upon the keys it
> was initialized with. i.e there could be another config object which will
> access another property "REGISTRATION_CURVE"..
>  config1.REGISTRATION_CURVE.
> 
> please lemme  know if theres any way this can be achieved in Python ?
> 
> Thank you for your time and help :)
> 
> Dhanvi
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582




More information about the Tutor mailing list