Question #606

Which keyword prevents changing variables in a nested function?

The keyword nonlocal indicates that a variable inside a nested function does not belong to the scope of that inner function but to the function encapsulating it. ``` def myfunc1(): x = "John" def myfunc2(): x = "hello" myfunc2() return x print(myfunc1()) ``` will output: ``` >>>John ``` The keyword global is different. It indicates that the scope of a variable is defined at the module level. (w3schools.com/python/ref_keyword_nonlocal.asp)


Comment on Disqus:

Comment on Twitter:

Question difficulty: 🔵🔵🔵🔵🔵