2.1.1 global weirds

Alex Martelli aleaxit at yahoo.com
Sat Aug 11 10:17:35 EDT 2001


"Robin Becker" <robin at jessikat.fsnet.co.uk> wrote in message
news:fHMWWLAyNTd7EwvM at jessikat.fsnet.co.uk...
    ...
> >> why is b2.getStory not getStory? Is it because importing from a isn't
> >> the same as the importing from the __main__ module?
> >
> >Exactly.  You have two instances of the module, one referred
> >to by sys.modules['__main__'] and the other by sys.modules['a'].
> >
> >> What is the correct way to import from the main script?
> >
> >import __main__
    ...
> not exactly good programming though if I later I import run from a in a
> different main script.

I agree: it's not good programming for any module to import
from the main script.  Dependencies should always be in the
other direction -- main script importing modules, not vice versa.


> Isn't this a wart?

It's surely IMHO a wart in your program if you need a module
to import from the main script.

If you're asking, is it a wart of Python that it allows you to
import the main script at all, my answer is that I don't think
so: Python is not in the business of enforcing good programming
practices.  Python gives you all the tools you need to structure
your programs well, and even provides a hint that "you really
shouldn't be doing this unless you really know what you're
doing" by having the main script's name decorated with
underscores -- now if you still want to go ahead and do it,
it's upon your head.

If you prefer a language that strives to enforce what its author
consider good programming practice, I would suggest Ada,
Eiffel, Modula-3, or Haskell.  Personally, I still prefer Python.


Alex






More information about the Python-list mailing list