pretty basic: get variable name from list of strings?

Chris Liechti cliechti at gmx.net
Mon Jul 29 20:18:11 EDT 2002


chris <> wrote in news:34kbku4j76104d193oevjfi83mk3olvsbt at 4ax.com:

> Thanks Andrew, 
> 
> I'll definitely look up how to use dicts.

an look at list when your at it. it will ease your life too...

> Will they also help in iterating other "treat string like code"
> situations like wxPython object creation, here where I want to create
> 100 radioboxes?
> 
> for j in range(1,100):
>      varname = 'self.Box'+str(j)
>      varname = wxRadioBox(self, ID_j, name, wxDefaultPos,
> wxDefaultSize, TeamList[j], 1, wxRA_SPECIFY_COLS)
>      EVT_RADIOBOX(self, ID_j, self.EvtRadioBox_j)

box = []    	#create an empty list
#and populate it:
for j in range(1,100):
    	 id = wxNewId()
      box = wxRadioBox(self, id, name, wxDefaultPos,
              wxDefaultSize, TeamList[j], 1, wxRA_SPECIFY_COLS)
      EVT_RADIOBOX(self, id, self.EvtRadioBox)
      self.boxes.append(box)

and EvtRadioBox can find out which was the event source, no need to write 
100 distict functions...

> Surely there's a way to have a string like 'FileCount = 6' and tell
> python to execute it.  Wait a minute...  execute...
> 
> Aha, 
>>>> exec('horse = 9')
>>>> horse
> 9

yes, but nobody told you that until now because there is always a better, 
more powerful way to do it.
 
chris (no, not the OP ;-)

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list