dynamically created names / simple problem

Robert Bossy Robert.Bossy at jouy.inra.fr
Tue Mar 25 11:20:00 EDT 2008


Robert Bossy wrote:
> Jules Stevenson wrote:
>   
>> Hello all,
>>
>> I'm fairly green to python and programming, so please go gently. The 
>> following code
>>
>> for display in secondary:
>>
>> self.("so_active_"+display) = wx.CheckBox(self.so_panel, -1, "checkbox_2")
>>
>> Errors, because of the apparent nastyness at the beginning. What I’m 
>> trying to do is loop through a list and create uniquely named wx 
>> widgets based on the list values. Obviously the above doesn’t work, 
>> and is probably naughty – what’s a good approach for achieving this?
>>
>>     
> Hi,
>
> What you're looking for is the builtin function setattr:
> http://docs.python.org/lib/built-in-funcs.html#l2h-66
>
> Your snippet would be written (not tested):
>
> for display in secondary:
>
> setattr(self, "so_active_"+display, wx.CheckBox(self.so_panel, -1, 
> "checkbox_2"))
Damn! The indentation didn't came out right, it should be:

for display in secondary:
    setattr(self, "so_active_"+display, wx.CheckBox(self.so_panel, 
-1,"checkbox_2"))

RB



More information about the Python-list mailing list