这是名为Test:
的模块的代码def scope_test():
def do_local():
spam = "local spam"
def do_nonlocal():
nonlocal spam
spam = "nonlocal spam"
def do_global():
global spam
spam = "global spam"
spam = "test spam"
do_local()
print("After local assignment:", spam)
do_nonlocal()
print("After nonlocal assignment:", spam)
do_global()
print("After global assignment:", spam)
然后我使用以下命令将文件导入Python Shell:
import Test
然后运行函数scope_test():
Test.scope_test
我也尝试过:
from Test import scope_test
但是我收到了这个错误: AttributeError:模块'测试'没有属性' scope_test'。 有人可以帮忙吗?非常感谢。