[Tutor] Control Variables and a Dictionary with a GUI Interface

Alan Gauld alan.gauld at btinternet.com
Mon Feb 16 19:24:44 CET 2009


"Wayne Watson" <sierra_mtnview at sbcglobal.net> wrote

> The question here is how is sdict being used here in terms
> of its need within the GUI?  Probably if I had written the
> program from scratch, I would have made all these
> variables global to Sentinel_GUI.

Most programmers try to avoid global variables as a
general principle. In this case the programmer has
opted for a dict and is then able to pass the values
around with a single variable, as in:

>    def DisplaySettings(self):
>        print "OSett self = ", self, "type =", type(self) <<---debug 
> code
>        sdict = {}                       <<-------- local sdict
>        sdict[ "ok" ] = False            <<---- ok
> ... more sdict ...
>        dialog = DisplaySettingsDialog( self.master, sdict )


> class DisplaySettingsDialog(tkSimpleDialog.Dialog):
>
>    def __init__(self, parent, sdict):
>        self.sdict = sdict               <<-------
>        tkSimpleDialog.Dialog.__init__(self, parent)

You could use globals if both classes were in the samemodule,
or import the module wherever they values were needed. But
by keeping the values in a dict and keeping the dict as a
class variable the programmer potentially has more control
over access.

Thats the only reason I can see for it  - after all of 2 minutes
reading the code :-)...


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list