I notice that global variables in Python (3.9.10) can be accessed within functions, but not mutated as shown below:
No error:
def absolute_value(number):
print(y)
if number < 0:
return -number
return number
y = 10
result = absolute_value(5)
Error:
def absolute_value(number):
y = y + 2
print(y)
if number < 0:
return -number
return number
y = 10
result = absolute_value(5)
I would like to know the more general mechanism - which is, without using the global
keyword, what's the most one can do with a global variable within a function's local namespace?