Python - What is the output: nested function and scope?

Question

What is the output of the following code?

X = 'Test' 
def func(): 
   X = 'test' 
   def nested(): 
       print(X) 
   nested() 

func() 
print(X) 


Click to view the answer

test
Test

Note

The print statement in the nested function finds the name in the enclosing function's local scope, and the print at the end finds the variable in the global scope.

Related Quiz