Converting string to argument

Brian Jones mojobojo at gmail.com
Fri Jul 2 13:14:42 EDT 2004


On 02 Jul 2004 08:08:59 -0400, Heather Coppersmith <me at privacy.net> wrote:

> The usual comp.lang.python answer is "why do you want to do this?"
> 
> In most cases, wanting to do this is a symptom of something else
> being sub-optimal.  Perhaps it's as simple as putting x and y into
> a dictionary instead of creating them as class attributes:

I think a good reason for wanting to map string values into literal
variables/methods/classes such as Joseph explained, is so that they
can be dynamically defined in something such as a config file, for
example.

I wrote a simple application which had a basic text based
configuration file, and a key/value pair defined as:

MODULES = mod1, mod2, ..modN

... where each module was seperated by commas, and read into the
application as strings.  getattr() and __import__() proved very useful
at this point, allowing me to dynamically load the modules, and the
classes within using the config string values.


-- Brian Jones




More information about the Python-list mailing list