storing variable names in a list before they are used?
Farshid Lashkari
no at spam.com
Fri Sep 29 16:06:51 EDT 2006
Hi John
John Salerno wrote:
> how would I go about putting these variable names in a list? I know I
> can't leave them as above, but if I put them in as a string, then how do
> I later "transform" them into an actual variable for assign, such as:
>
> first_name = widget.get_text()
>
> Is there some kind of idiom that does this sort of work?
What scope do you want these variable names to show up in? For example,
if you want them to be defined in the global scope then you can do the
following:
name = 'first_name'
globals()[name] = widget.get_text()
print first_name
If you want these variables to be assigned to an object then you can use
setattr():
name = 'first_name'
setattr(obj,name,widget.get_text())
print obj.first_name
Hope this helps
-Farshid
More information about the Python-list
mailing list