[wxPython] "thisModule = __import__(__name__, globals())" statement used in pyTree.py demo...

G'day,

I just noticed the above statement--
    thisModule = __import__(__name__, globals())
if your subject line isn't properly visible--in pyTree.py, when hacking
around with the demo. The thisModule variable isn't used at all, and my
(perhaps very limited) understanding of the __import__ function is that this
function has no "side effects"--it will not change anything in globals()
[besides which, I think it's return value may be merely a COPY of the
globals() dictionary... either that or locals(), or both; can't remember at
the moment...]. What is the purpose of this statement, then? I tried
commenting out the statement, and the demo seemed to have gone into ga-ga
land [in that control + escape brought up the start menu (by the way, I'm
using Windows 98), a sign (I think so, anyway) that I'd better control +
alt + delete the process darn quickly or risk having to reboot...], without
bringing up the pyTree window. So the statement definitely DOES something,
and something important... but what?

This MIGHT be related to a problem I've had for well over a year, in that
global variables (there! I said it! Excuse me ::sunglasses: of a module aren't
referenced within "callbacks" [e.g., instance methods of the form
'OnXXX(self, event)'], even when referenced in an appropriate 'global'
statement--one needs something like 'from MODULE import IDENTIFIER' instead,
which is very ugly--particularly when the module contains classes which are
to be used both when it is imported by another module, and used within a 'if
__name__ == "__main__"' construct (in which case a statement similar to
"sys.modules['MODULE'] = sys.modules['__main__']" will be necessary as a
workaround. Very, very ugly...

[Actually, not just in "callbacks" in the above paragraph, but also in
__init__ methods, for example...]

I've read and re-read the Python manuals on this matter. Can anyone throw
some light on either of these matters?

Thanks, Chris

···

-------------------------------------------------------------------------
Chris Fama <mailto:Chris.Fama@whollysnakes.com> Phone:(07) 3870 5639
Brisbane, Australia Mobile:(0400) 833 700
-------------------------------------------------------------------------
-o0= Strange but true... (?!) =0o-
The shape of plant collenchyma cells and the shape of the bubbles in
beer foam are the same - they are orthotetrachidecahedrons.

I just noticed the above statement--
    thisModule = __import__(__name__, globals())
if your subject line isn't properly visible--in pyTree.py, when hacking
around with the demo. The thisModule variable isn't used at all,

Actually it is. Look further down about 3 lines.

and my
(perhaps very limited) understanding of the __import__ function is that

this

function has no "side effects"--it will not change anything in globals()

From the Python docs: """...and uses its globals only to determine the

package context of the import statement."""

[besides which, I think it's return value may be merely a COPY of the
globals() dictionary... either that or locals(), or both; can't remember

at

the moment...]. What is the purpose of this statement, then?

To get a reference to the module object that contians the pyTree code. (The
demo uses it as a namespace for populating the tree.) It couls also have
just done "import pyTree" but then it would have problems if the module ever
changed names. Perhaps a better way to do it would have been

    thisModule = sys.modules[__name__]

This MIGHT be related to a problem I've had for well over a year, in that
global variables (there! I said it! Excuse me ::sunglasses: of a module aren't
referenced within "callbacks" [e.g., instance methods of the form
'OnXXX(self, event)'], even when referenced in an appropriate 'global'
statement--one needs something like 'from MODULE import IDENTIFIER'

instead,

Is MODULE the module the current code is in or is it another module where
the global is at? If the former I can't duplicate it so please send a
sample. If the latter then that is the normal ordinary Python LGB (Locals,
Globals, Builtins) scope rule which defines where and in what order
unqualified names are searched for. Globals are not application wide, but
basically means names defined at the top level of the module rather than
names in the local scope or in the __builtins__ module.

If you are trying to get application wide "global variables" then the way to
do it is to have a module (perhaps named globals) that shouldn't have any
side effects when being imported other than initializing the global
vaiables. For example:

    one = 1
    two = 2
    three = "one and one and one"

Then every module that wants to use the globals just imports it (not it's
contents) and uses the module as a "global namespace"

    import globals
    print globals.one
    globals.three = 3

HTH,

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

> I just noticed the above statement--
> thisModule = __import__(__name__, globals())
> if your subject line isn't properly visible--in pyTree.py, when hacking
> around with the demo. The thisModule variable isn't used at all,

Actually it is. Look further down about 3 lines.

<Embarrassed look> Sorry!

>
> This MIGHT be related to a problem I've had for well over a
year, in that
> global variables (there! I said it! Excuse me ::sunglasses: of a module aren't
> referenced within "callbacks" [e.g., instance methods of the form
> 'OnXXX(self, event)'], even when referenced in an appropriate 'global'
> statement--one needs something like 'from MODULE import IDENTIFIER'
instead,

Is MODULE the module the current code is in or is it another module where

[Yes it is)

the global is at? If the former I can't duplicate it so please send a
sample.

I'll try to put something together if I don't get it working vis-a-vis your
later suggestion.. Actually, that suggestion is along the lines of something
I did a year or so ago, with moderate success... I'd love to isolate and
identify the problem, though... [bit like wanting to have one's cake and eat
it too, yeah?!--Without having to bake it first! :8-]

Thanks a lot!

Chris