python中设计一个在全局作用域、嵌套作用域、局部作用域存在部分相同变量名的函数;在最内部函数(myfun3)中,打印myfun3中的变量值;并修改对myfun3函数而言的嵌套作用域和全局作用域中同名变量的值,并在相应的作用域中打印出修改后的变量的值。
时间: 2023-06-02 17:04:01 浏览: 121
此问题属于技术问题,我可以回答。以下是实现代码:
```
var_global = "I am in the global scope"
def myfun1():
var_nonlocal = "I am in the nested scope"
print(var_global, var_nonlocal) # prints "I am in the global scope I am in the nested scope"
def myfun2():
var_local = "I am in the local scope"
print(var_global, var_nonlocal, var_local) # prints "I am in the global scope I am in the nested scope I am in the local scope"
def myfun3():
var_local = "I am in the inner local scope"
nonlocal var_nonlocal
global var_global
var_nonlocal = "I am modified in inner function"
var_global = "I am modified in inner function too"
print(var_local)
print(var_nonlocal)
print(var_global)
myfun3()
myfun2()
myfun1()
```
输出结果:
```
I am in the global scope I am in the nested scope
I am in the global scope I am in the nested scope I am in the local scope
I am in the inner local scope
I am modified in inner function
I am modified in inner function too
```
阅读全文