[Tutor] Issue with iterating within lists, objects and functions

Kent Johnson kent37 at tds.net
Sat Aug 4 05:00:08 CEST 2007


Ricardo Aráoz wrote:
> Kent Johnson wrote:
>> Andy Cheesman wrote:
>>> for thing in ["top", "right", "bottom", "left"]:
>>> 	eval("self." + thing).append("fish")
>> You don't need to use eval() here, use getattr() instead:
>> getattr(self, thing).append('fish')
>>
> 
> What if you want to do :
> MyObj = 1
> myStr = 'MyObj'
> eval('MyObj') = 125

Usually a dictionary is the right solution, instead of a named variable.

values = {'MyObj' : 1}
values[myStr] = 125

Kent


More information about the Tutor mailing list