Q
What does this Python code print?
def outer():
n = 0
def inner():
nonlocal n
n += 1
inner(); inner()
return n
print(outer())
-
A
1
-
C
2
-
D
UnboundLocalError
Correct Answer:
C. 2
Explanation:
nonlocal lets inner rebind n in the enclosing function's scope, so n is incremented twice.