2.1.1 global weirds

Robin Becker robin at jessikat.fsnet.co.uk
Sat Aug 11 17:35:00 CEST 2001


In article <9l3f170adp at enews1.newsguy.com>, Alex Martelli
<aleaxit at yahoo.com> writes
>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.

well I didn't write the write the original of the problem causing
scripts and have now rewritten exactly in the way you suggest. 

The real wart I feel is that the module a has two names, one when run as
a script and the other when imported. 

I suppose there is some overriding implementation issue, but it seems to
me that in the file a.py

__file__ and __name__ ought always to be defined and given the
appropriate 'right' values.

Since magic variables/names seem to be natural in python I would just
make __main__ true in the main script and false everywhere else. It's
truth value could be the module that corresponds to the main script.

That would break all the if __name__=='__main__' scripts so it could
never be done.
-- 
Robin Becker



More information about the Python-list mailing list