atom feed5 messages in org.python.python-listwhy import * only allowed at module l...
FromSent OnAttachments
Simo SalminenJan 13, 2002 8:06 pm 
Hans NowakJan 13, 2002 8:38 pm 
Simo SalminenJan 13, 2002 9:33 pm 
Peter HansenJan 13, 2002 10:04 pm 
Cesar DouadyJan 14, 2002 2:28 am 
Subject:why import * only allowed at module level?
From:Hans Nowak (wur@earthlink.net)
Date:Jan 13, 2002 8:38:51 pm
List:org.python.python-list

Simo Salminen wrote:

How come python 2.2 does not allow code like this:

def a():

... from os import * <interactive input>:1: SyntaxWarning: import * only allowed at module level

Why this was changed? I like to do import * at function level because it is handy but doesn't pollute namespace.

See

http://amk.ca/python/2.1/index.html

the section about nested scopes.

"One side effect of the change is that the from module import * and exec statements have been made illegal inside a function scope under certain conditions. The Python reference manual has said all along that from module import * is only legal at the top level of a module, but the CPython interpreter has never enforced this before. As part of the implementation of nested scopes, the compiler which turns Python source into bytecodes has to generate different code to access variables in a containing scope. from module import * and exec make it impossible for the compiler to figure this out, because they add names to the local namespace that are unknowable at compile time."

--Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA==') # decode for email address ;-) Site:: http://www.awaretek.com/nowak/