Unable to access the modified value of a global variable from outside the function: Variable not updated

Assuming the following snippet:

a = None

def set_a():
    global a
    a = 10+2

print(a)

The result is expected to be 12, so why does it remain as None and won’t update?
I’ve been looking for similar questions on stackoverflow but didn’t find a proper explanation/solution. How exactly can I access the modified value of a global variable which has been updated inside a function from outside of it?
Any help is appreciated in advance.

>Solution :

>>> a = None
>>> 
>>> def set_a():
...     global a
...     a = 10+2
... 
>>> set_a()
>>> 
>>> print(a)
12

Leave a Reply