How to guard against bugs like this one?
Carl Banks
pavlovevidence at gmail.com
Wed Feb 3 17:24:59 EST 2010
On Feb 3, 8:55 am, Nobody <nob... at nowhere.com> wrote:
> On Tue, 02 Feb 2010 10:38:53 -0800, Carl Banks wrote:
> >> I don't know if that's necessary. Only supporting the "foo.h" case would
> >> work fine if Python behaved like gcc, i.e. if the "current directory"
> >> referred to the directory contain the file performing the import rather
> >> than in the process' CWD.
>
> >> As it stands, imports are dynamically scoped, when they should be
> >> lexically scoped.
>
> > Mostly incorrect. The CWD is in sys.path only for interactive
> > sessions, and when started with -c switch. When running scripts, the
> > directory where the script is located is used instead, not the
> > process's working directory.
>
> Okay, so s/CWD/directory containing __main__ script/, but the general
> argument still holds.
>
> > So, no, it isn't anything like dynamic scoping.
>
> That's what it looks like to me. The way that an import name is resolved
> depends upon the run-time context in which the import occurs.
Well it has one superficial similarity to dynamic binding, but that's
pretty much it.
When the directory containing __main__ script is in the path, it
doesn't matter if you invoke it from a different directory or os.chdir
() during the program, the same modules get imported. I.e., there's
nothing dynamic at all about which modules are used. (Unless you
fiddle with the path but that's another question.)
All I'm saying is the analogy is bad. A better analogy would be if
you have lexical binding, but you automatically look in various sister
scope before your own scope.
Carl Banks
More information about the Python-list
mailing list