variable hell
Ron Garret
rNOSPAMon at flownet.com
Thu Aug 25 12:08:14 EDT 2005
In article <mailman.3508.1124979630.10512.python-list at python.org>,
Benji York <benji at benjiyork.com> wrote:
> Peter Maas wrote:
> > >>> suffix = 'var'
> > >>> vars()['a%s' % suffix] = 45
> > >>> avar
> > 45
>
> Quoting from http://docs.python.org/lib/built-in-funcs.html#l2h-76 about
> the "vars" built in:
>
> The returned dictionary should not be modified: the effects on the
> corresponding symbol table are undefined.
If you really want to make something like this work you can define a
class that would work like this:
vars = funkyclass()
varname = 'x'
vars[varname] = value
vars.x
But this is clearly a design mistake. Either you know the names of the
variables when you write the code or you do not. If you know them you
can simply assign them directly. If you do not know them then you can't
put them in the code to read their values anyway, and what you need is
just a regular dictionary.
rg
More information about the Python-list
mailing list