don't use 'from' (was Re: module variable scope)

Stefan Quandt squan at web.de
Wed Oct 29 03:52:24 EST 2003


Alex Martelli <aleax at aleax.it> wrote in message news:<8tunb.366820$R32.12095727 at news2.tin.it>...
> Stefan Quandt wrote:
>    ...
> > <begin b.py>
> > from a import v, setv, getv
> 
> The from statement takes a "snapshot" at that instant of time of
> the values currently bound to those names in the other module,
> binding each value to the same name in the current module.
> 
> That's all.  It establishes no magical "persistent linkage
> between names in different modules" -- there exists no concept
> of such magical persistent linkage in Python.
> 
> Any rebinding of those or other names in the other module in
> the future will be totally irrelevant to the binding of said
> names in the current module.
> 
Oh, I thought that 'from' is a means to explicitly import selected
symbols from a module instead of importing the whole.
I never was aware of the different semantics of using 'from' in
imports.
In this case python does not work like one would intuitively expect.

Of course one could find a hint in the reference manual 6.12:
The from form does not bind the module name: it goes through the list
of identifiers, looks each one of them up in the module found in step
(1), and binds the name in the local namespace to the object thus
found.

Thanks
  Stefan




More information about the Python-list mailing list