Why is the global variable inaccessible from the function?
📁 function, Advanced Python Topics, AI
Please tell me why the function does not display the value of s
s = ""I'm a global variable""
def foo():
print(s)
s = ''reset global variable'
foo()
I assumed that when the interpreter reaches the print(s) line, it will understand that in the scope of the foo function there is no such variable. Therefore, it will start looking for it in the external scope, but there it is.
However, as a result, the following error message is displayed in the console:
Traceback (Most Recent Call Last):
File "<stdin>", line 3, in foo
NameError: name 's' is not defined
No answers yet.
Log in to leave an answer
Blogs