module question

Aahz Maruch aahz at panix.com
Mon Jan 1 20:34:14 EST 2001


In article <3A5118AB.BDE840D5 at uniserve.com>,
Bob van der Poel  <bvdpoel at uniserve.com> wrote:
>
>In my main program I start off with a
>
>	from Tkinter import *
>	from edit import Edit
>
>Now, in the main program I call 'Edit', but it fails. Seems that I have
>to insert a 'from Tkinter import *' in 'Edit' as well. So, does this
>mean that each and every module needs to re-import all the 'global-type'
>stuff? I suppose this aids in keeping data private, etc. but must add a
>lot of overhead at the same time. Maybe I should keep my monolithic
>program structure?

If your edit module accesses Tkinter functionality, it does need the
"from Tkinter import *".  However, it does not add much in the way of
overhead to import the same module into multiple user modules.
(Consider how often you have to "import string", for exmaple.)

>Now, to complicate my understanding even more...if I declare a variable
>in the 'main' module or program how do I access it from imported modules?
>For example, I might have something like:
>
>	versionNumber = 1.4
>
>in the mainline. Now, a module (and I guess it might be deeply
>nested), wants to find out what version is running. I just tried a
>few variations of 'global', but nothing seems to work. Passing this
>variable around to everyone who might need it gets real old, real
>fast...so I'm thinking I'm not understanding something!

Ordinarily I'd recommend against this, but this seems like it might be
an appropriate use.  In your edit module, do this:

import __main__
print __main__.versionNumber


OTOH, that's such a horrid kludge that what you *really* should do is
have a config module of some sort where you store the version number and
import *that* into all your modules (including main).
-- 
                      --- Aahz (Copyright 2000 by aahz at pobox.com)

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

Tenth Virtual Anniversary: 364 days and counting



More information about the Python-list mailing list