Why do directly imported variables behave differently than those attached to imported module?

Chris Angelico rosuav at gmail.com
Tue May 3 17:47:17 EDT 2011


On Wed, May 4, 2011 at 2:57 AM, Chris Rebert <clp2 at rebertia.com> wrote:
> from foo import *
>
> can be thought of as essentially doing:
>
> import foo
> set = foo.set
> var = foo.var
> del foo

Here's a side point. What types will hold a reference to the enclosing
module (or at least its dictionary)? Would it be possible to use a
from import to load a module, then "lose" the module even though
you're using objects from it?

I am guessing that a function or class will hold such a reference,
because otherwise it would be a bit awkward for them to use any sort
of module-level state. Or do they not, and instead go look for their
module in sys.modules?

Chris Angelico



More information about the Python-list mailing list