Creating a local variable with a parameterized name
Michael Ross
mross at rochester.rr.com
Sat Jan 29 17:57:57 EST 2000
I'm new to this, so I hope I'm not missing something obvious...
I want to create a local variable with a passed in name.
I realize I can do the following:
>>> def f(varname):
cmd = '%s = 123' % varname
exec(cmd)
cmd = 'print %s' % varname
exec(cmd)
>>> f('mike')
123
>>>
But exec needs to invoke the parser, which seems to be an extremely
costly thing to do.
It seems local variables are stored in a special, inaccesible (from
python) place in the frame object (I've been looking at
PyFrame_FastToLocals and PyFrame_LocalsToFast). Do I need to write C
code to do this? A builtin to do this would be nice...
Thanks in advance!
Mike Ross
More information about the Python-list
mailing list