[Tutor] Creating an Identifier or Object Name from a String?

Dan Shafer pydan@danshafer.com
Tue, 11 Jun 2002 01:02:33 -0700


I have a need to refer to a number of objects which have been named field1, 
field2, field3, etc. I want to set a property in each object in a loop.

I thought this should work:

   for ct in range(1,4):
     objToUpdate = "field" + str(ct)
     objToChange = eval(objToUpdate) # seems like it should produce 
"field1" first time through the loop, etc.
     objToChange.text = inputList[ct] #inputList is generated prior to 
entering the loop and is a list of string values

This produces an error indicating that string objects don't have a text 
attribute. So clearly objToChange is still a string at this point.

I suspect this is tricky but if someone could help....

Dan Shafer, Chief Scribe and Tablet Keeper
PythonCard Open Source Project
http://pythoncard.sourceforge.net