From: Jeremy Hylton <jeremy@alum.mit.edu>
"SP" == Samuele Pedroni <pedronis@bluewin.ch> writes:
SP> Hi. Q about PEP 267 Does the PEP mechanims adress only SP> import a SP> use a.x
SP> cases. How does it handle things like SP> import a.b SP> use a.b.x
You're a smart guy, can you tell me? :-). Seriously, I haven't gotten that far.
import mod.sub creates a binding for "mod" in the global namespace
The compiler can detect that the import statement is a package import -- and mark "mod.sub" as a candidate for optimization. A use of "mod.sub.attr" in function should be treated just as "mod.attr".
The globals array (dict-list hybrid, technically) has the publicly visible binding for "mod" but also has an internal binding for "mod.sub" and "mod.sub.attr". Every module or submodule attribute in a function gets an internal slot in the globals. The internal slot gets initialized the first time it is used and then shared by all the functions in the module.
So I think this case isn't special enough to need a special case.
OK, I stated the wrong question. What happens if I do the following: import a.b def f(): print a.b.x a.g() print a.b.x f() Now a.g() change a.b from a submodule to an object with a x attribute. Maybe this case does not make sense, but the point is that the PEP is quite vague about imported stuff. Samuele (more puzzled than smart).