这按预期工作
def outer_func():
from time import *
print time()
outer_func()
我可以在上下文中定义嵌套函数并从其他嵌套函数中调用它们:
def outer_func():
def time():
return '123456'
def inner_func():
print time()
inner_func()
outer_func()
我什至可以导入单个函数:
def outer_func():
from time import time
def inner_func():
print time()
inner_func()
outer_func()
然而,这会抛出SyntaxError: import * is not allowed in function 'outer_func' because it contains a nested function with free variables
:
def outer_func():
from time import *
def inner_func():
print time()
inner_func()
outer_func()
我知道这不是最佳实践,但为什么它不起作用?