[Tutor] Dynamic-ish naming of variables?

Liam Clarke cyresse at gmail.com
Mon Nov 22 15:11:24 CET 2004


Hi Alan,

Feels pretty magical when I 'get' that concept, and I find myself
wishing I could study this fulltime to 'get' it quicker. Oh well, one
day at a time.

Regards,

Liam Clarke

On Mon, 22 Nov 2004 10:55:23 -0000, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
> > Well, here's why. ...
> 
> Nope, not yet.
> 
> 
> 
> > {'type':'CheckBox',
> >     'name':'LiamClarkeCheck',
> >     'position':(104, 71),
> >     'checked':True,
> >     'label':'CheckBox1',
> >     },
> >
> > Python checks values assigned by the following -
> >
> > if self.components.LiamClarkeCheck.checked=True ....
> >
> > How do I do the name part randomly, short of having my module
> re-write itself?
> 
> if self.components.checkboxes['LiamClarkCheck'].checked = True
> 
> does the same thing using a dictionary.
> 
> > Ideally it would iterate over a list, something along these
> pseudo-lines -
> >
> > for name in nameList:
> >
> >      if self.components.%sCheck.checked=True % name
> >      teamDict[name]=self.components.%sChoice.field % name
> >
> 
> cb = self.comonents.checkBoxes  # saves typing!
> for name in nameList:
>     if cb[name].checked = True
>     teamDict[name] = cb[name].field
> 
> > Any suggestions? Could I do something like the above and use eval /
> > exec? I'm not too familiar with them.
> 
> No magic just a dictionary for the checkbox components.
> Remember that Python uses dictionaries for all its variable
> name handling interally so whatever python does with names
> internally you can pretty much do explicitly by adding an
> explicit dictionary.
> 
> HTH
> 
> Alan G
> Author of the Learn to Program web tutor
> http://www.freenetpages.co.uk/hp/alan.gauld
> 
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list