I have a local variable x = "local"
which unfortunately shares its name with both a global and a non-local variable. Without changing any of the names, can I access all three values? For x = "global"
there is globals()
, but what about the non-local variable?
Minimal example which illustrates the issue:
x = "global"
def f(x="nonlocal"):
def g():
x = "local"
print(x) # same as locals()["x"]
print(globals()["x"])
# here I want to print the non-local x
return g
f()()