これは期待どおりに機能します
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()
これがベストプラクティスではないことは承知していますが、なぜ機能しないのですか?