Sharing variable among files?
-
If i declare a variable in boot.py is available to all files?
-
@misterlisty
yes -
thank you
-
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?
-
variable scope
yes, you must do this with
globalkeyword
e.g.:def myproc(): global my_global_var my_global_var = 1 -
@livius That does not work for me. Did you try that?
-
@robert-hh
i was too fast, you have right global is for same unit
simple use qualified name e.g:a.pymyvar = 7b.pyimport 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
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login