Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading