我可以访问特定范围内的其他变量吗?像这样:
variable = 'acces with global'
def function1:
variable = 'function 1 variable'
def function2:
variable = 'I really like this name for variable'
def function3:
def access_to_local:
variable = "it's another variable I can access too!"
def access_to_global:
global variable
variable = 'now I have access to my global variable'
def access_to_function2:
nonlocal variable
variable = 'And easy way to change function2 variable'
def access_to_function1:
#nonlocal^2 variable?
variable = 'And how can I get access to variable in
function1?'
这不是生死问题,我只是好奇。我只是学习了 python 中的全局和非本地如何工作。现在,这绝对足够了,但我想知道我所要求的是否可能。特别是,我读到了 pep,他们正在考虑制作“特定范围内的非本地”之类的东西。但他们决定改用非本地的。是不是因为有办法在没有非本地的情况下做到这一点?或者也许有一些非本地的技巧,比如写两次nonlocal nonlocal variable
?