[Tutor] Dynamic-ish naming of variables?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Nov 22 09:35:15 CET 2004



On Mon, 22 Nov 2004, Liam Clarke wrote:

> Python checks values assigned by the following -
>
> if self.components.LiamClarkeCheck.checked=True ....

Hi Liam,


Hmmm!  I haven't played with PythonCard that much yet.  According to the
documentation on:

 http://pythoncard.sourceforge.net/framework/general_concepts_and_limitations.html

"components" is a dictionary-like object, with extra syntactic sugar so
that we can use dot notation.  That means that, hopefully, we're pretty
much home free, because it's fairly easy to do dynamic inserts into a
dictionary without much magic.


I believe the 'components' attribute is an instance of the 'WidgetDict'
class, from looking at the code of PythonCard's data model:

http://cvs.sourceforge.net/viewcvs.py/pythoncard/PythonCard/model.py?rev=1.190&view=auto


> 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


You should actually be able to say something like:

###
for name in nameList:
    if self.components[name].checked = True:
        teamDict[name] = self.components[name].checked
###


I have not tested this yet though.  I'm going to regret saying that,
aren't I?  *grin* I just don't have access to wxPython at the moment.


If you have more questions, please feel free to ask!



More information about the Tutor mailing list