'Global variable is not updated if it is from other .py file

As you know, the code:

var = "flower"
def update():
    global var
    var = "forest"

update()
print(var)

will properly modify the variable var, but if you do in from other file, like :

#global_var_update.py file:

var = "flower"

def update():
    global var
    var = "forest"

#update()
print(var)

And other file:

from global_var_update import *


update()
print(var)

this will Not update the variable( it will remain "flower" What is the reason of this behavior and how to have this funtionality properly? thanks for answers.



Solution 1:[1]

Well, using the Global method is a very bad idea ;-; if you want it to work correctly, you should write your function in the form of :

#global_var_update.py file:
var = "flower"
def update(vur):
   vur = "forest"
   return vur

And other file:

from global_var_update import *


update(var)

Hope it'll help you :D

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shiga