[Python-ideas] Allow imports to a global name

Terry Reedy tjreedy at udel.edu
Wed Apr 11 07:08:50 CEST 2012

On 4/10/2012 8:59 PM, Nick Coghlan wrote:
> On Wed, Apr 11, 2012 at 10:36 AM, Guido van Rossum<guido at python.org>  wrote:
>> +1
> Ditto. FWIW, I'm actually in favour of dropping everything after the
> "or" in that paragraph from the language spec, since we don't enforce
> *any* of it. Aside from formal parameter definitions (which explicitly
> declare local variables), name binding operations are just name
> binding operations regardless of the specific syntax.
> With global:
>>>> def f():
> ...   global x
> ...   for x in (): pass
> ...   class x: pass
> ...   def x(): pass
> ...   import sys as x
> ...
>>>> f()
>>>> x
> <module 'sys' (built-in)>

I found this slightly surprising, but on checking with dis, each of the 
'implicit' assignments is implemented as the equivalent of x = 
internal_function_call(args).  (For for-loops, the assignment is within 
the loop.) Which is to say, each bytecode sequence ends with store_xxx 
where xxx is 'fast' or 'global. So now I am not surprised.

Terry Jan Reedy

More information about the Python-ideas mailing list