name-spaces and loading a file

Aahz Maruch aahz at netcom.com
Fri Feb 18 11:06:22 EST 2000


In article <38AD44E2.637FBFF7 at austin.ibm.com>,
David R. Favor <dfavor at austin.ibm.com> wrote:
>
>Specifically, in the case of:
>
>   exec 'from scenes.' + scene + ' import *'
>
>Do you mean that within each $scene class all methods pass a dictionary
>as one of the arguments in method calls?

Nope.  Remember that I said the dict gets passed at __init__.  From that
point forward, all methods access it with self.dict.  So you can do
things like

    if self.dict.has_key(foo):

or

    self.dict[foo] = bar

and this information is globally accessible to all the scene class
instances.

Note very carefully that in this system, one is *NOT* doing that exec
any more.  Instead one is doing something like (pardon any syntax or
semantic errors; this isn't the way I'd write it):

sceneObjects = {}
globalDict = {}
exec 'import scenes.' + scene
exec 'sceneObjects[' + scene + '] = scenes.' + scene + '(globalDict)'

Now you can refer to the scene object (class instance) by string name,
and each scene object can refer to the global variables by string name
within globalDict.
--
                      --- Aahz (Copyright 2000 by aahz at netcom.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

Our society has become so fractured that the pendulum is swinging
several different directions at the same time



More information about the Python-list mailing list