Skip to content

Instantly share code, notes, and snippets.

@ssanderson
Created September 30, 2015 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssanderson/1067c7ac267aedfa7865 to your computer and use it in GitHub Desktop.
Save ssanderson/1067c7ac267aedfa7865 to your computer and use it in GitHub Desktop.
Import * inside an inner function fails only if that function references variables defined outside the function.
>>> def outer():
... def inner():
"Fails with SyntaxError"
... from numpy import *
... return array
... return inner()
...
<stdin>:2: SyntaxWarning: import * only allowed at module level
File "<stdin>", line 3
SyntaxError: import * is not allowed in function 'inner' because it is a nested function
>>> def outer():
... def inner():
"Warns, but doesn't fail!"
... array = 3
... from numpy import *
... return array
... return inner()
...
<stdin>:2: SyntaxWarning: import * only allowed at module level
>>> outer()
<built-in function array>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment