Sharing variable among files?
-
If i declare a variable in boot.py is available to all files?
-
@robert-hh
i was too fast, you have right global is for same unit
simple use qualified name e.g:a.py
myvar = 7
b.py
import a def changea(): a.myvar+= 2 print(a.myvar) def printa(): print(a.myvar)
for test use
import b b.changea() >>>9 b.print(a) >>>9
-
@livius That does not work for me. Did you try that?
-
variable scope
yes, you must do this with
global
keyword
e.g.:def myproc(): global my_global_var my_global_var = 1
-
ok, this works fine but it appears that if i chnage the value of this variable within a function then it appears to not be reflected within other function or files? I assume this has something to do with variable scope?
-
thank you
-
@misterlisty
yes