pretty basic: get variable name from list of strings?
Max M
maxm at mxm.dk
Tue Jul 30 06:07:17 EDT 2002
Terry Reedy wrote:
> g = globals() #
> for i in range(len(horses)):
> g['horse_'+str(i)] = horses[i]
>
> However, I strongly question whether you *really* want to do that.
> Accessing a list of names such as horses thru an index is much easier
> that thru a dict keyed with sequential strings. Cluttering globals is
> also a bad idea, and above will not work with locals().
Not using a dicts and trying to get variables from generated variable
names can give all kind of funny side effects.
What if somebody passes a list like:
horses = ['str', 'int', 'float', 'dict', 'repr'] # etc ...
This will certainly cause unintended behaviour.
regards Max M
More information about the Python-list
mailing list