mirror of
https://github.com/micropython/micropython.git
synced 2025-12-16 01:40:14 +01:00
tests/basics: Add further tests for nonlocal scoping and closures.
This commit is contained in:
@@ -19,3 +19,25 @@ def f():
|
||||
g()
|
||||
return a
|
||||
print(f())
|
||||
|
||||
# nonlocal at inner-inner level (h)
|
||||
def f():
|
||||
x = 1
|
||||
def g():
|
||||
def h():
|
||||
nonlocal x
|
||||
return x
|
||||
return h
|
||||
return g
|
||||
print(f()()())
|
||||
|
||||
# nonlocal declared at outer level (g), and referenced by inner level (h)
|
||||
def f():
|
||||
x = 1
|
||||
def g():
|
||||
nonlocal x
|
||||
def h():
|
||||
return x
|
||||
return h
|
||||
return g
|
||||
print(f()()())
|
||||
|
||||
Reference in New Issue
Block a user