[Python-Dev] avoiding accidental shadowing of top-level libraries by the main module (original) (raw)
Steven D'Aprano steve at pearwood.info
Tue Jul 13 02:30:30 CEST 2010
- Previous message: [Python-Dev] avoiding accidental shadowing of top-level libraries by the main module
- Next message: [Python-Dev] avoiding accidental shadowing of top-level libraries by the main module
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Tue, 13 Jul 2010 08:05:24 am Nick Coghlan wrote:
On Tue, Jul 13, 2010 at 7:47 AM, Fred Drake <fdrake at acm.org> wrote: > On Mon, Jul 12, 2010 at 5:42 PM, Michael Foord > > <fuzzyman at voidspace.org.uk> wrote: >> I'm sure Brett will love this idea, but if it was impossible to >> reimport the script being executed as main with a different >> name it would solve these problems. > > Indeed! And I'd be quite content with such a solution, since I > consider scripts and modules to be distinct.
And here I've been busily blurring that distinction for years ;) (actually, the whole "name == 'main'" idiom meant the distinction was already pretty blurry long before I got involved)
I would hate it if that distinction was un-blurred. Most of my modules include a section "if name == 'main': run_tests(), and some of them do significantly more than that. A few of them import themselves so they can pass the module object to another module.
I take it the concrete proposal here is if the filename of a new module matches either main.file or main.cached, then that module should be ignored completely for import purposes allowing a module with the same name later on sys.path to be found?
I'm not sure I like that, I'd be more inclined to just return the main module in that case rather than letting the search continue further down sys.path (although I agree the current semantics of getting two copies of the same module under different names in this case are less than ideal).
Yes, that's a weird corner case. I don't see any advantage to keeping that behaviour.
-- Steven D'Aprano
- Previous message: [Python-Dev] avoiding accidental shadowing of top-level libraries by the main module
- Next message: [Python-Dev] avoiding accidental shadowing of top-level libraries by the main module
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]