Python Forum
change value of a global variable across all modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
change value of a global variable across all modules
#1
hello

I wrote a minimum working example showing what I am unable to achieve:

I have a variable in one.py called foo, i would like to change it's value and then use the new value accross all modules

one.py
import two

foo = 'a'

def main():
    two.change_foo()
    print(foo)
    
if __name__ == "__main__":
    main()
two.py
import one

def change_foo():
    print(one.foo)
    one.foo = 'b'
    print(one.foo)
output:
Quote:Reloaded modules: two, one
a
b
a

desired output:
Quote:a
b
b
Reply
#2
If you run one.py it is not run as module one but as module __main__. The import one in module two imports a new copy of the execution of one.py. That's why your code doesn't work.
Reply
#3
(Dec-31-2018, 01:12 PM)Gribouillis Wrote: If you run one.py it is not run as module one but as module __main__. The import one in module two imports a new copy of the execution of one.py. That's why your code doesn't work.

and how i could solve this? how could i make the import one in module two write a new value in one.py?
Reply
#4
https://stackoverflow.com/questions/3536...her-module
it seems to be impossible... how could i to something similar?

edit: this seems to work
one.py:
import two

foo = None

def main():
    print(foo)
    two.change_foo()
    print(two.get_foo())
    
if __name__ == "__main__":
    main()
two.py:
import one

def change_foo():
    one.foo = 'b'

def get_foo():
    return one.foo
better ideas?
Reply
#5
You need to distinguish the main program from the library modules that this program imports. These library modules should not need to
import items from the main module. In this case I would define two modules, 'one' and 'two' and write the main program in a separate file
#=========================
# FILE one.py
foo = 'a'

#=========================
# FILE two.py
import one

def change_foo():
    one.foo = 'b'

#=========================
# FILE program.py
import one
import two

def main():
    print(one.foo)
    two.change_foo()
    print(one.foo)

if __name__ == '__main__':
    main()
Alternately, you can set data stored in the main program from imported modules if you pass references to these data to the given modules. For example
# FILE two.py
def change_foo(one): # <- Notice the parameter here. It will be provided by the function's caller
    one.foo = 'b'

#=========================
# FILE program.py
import two

class One:
    foo = 'a'

def main():
    print(One.foo)
    two.change_foo(One)
    print(One.foo)

if __name__ == '__main__':
    main()
Reply
#6
thanks i liked the first method and created a file settings.py where i store all this kind of information, i would use this approach also in the future!
Have a good new year :D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,147 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Help with variable in between modules llxxzz 5 818 Jul-31-2023, 04:20 PM
Last Post: Gribouillis
  how can a variable change if I haven't changed it? niminim 5 3,029 Apr-07-2021, 06:57 PM
Last Post: niminim
  Variable scope - "global x" didn't work... ptrivino 5 3,028 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Change variable value during a while loop? penahuse 2 4,040 Nov-15-2020, 11:53 PM
Last Post: penahuse
  Change variable in an outside file ebolisa 5 2,616 Nov-11-2020, 04:41 AM
Last Post: ebolisa
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,193 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  Change name of variable samuelbachorik 2 2,044 Aug-10-2020, 02:34 PM
Last Post: deanhystad
  NameError for global variable leviporton 3 2,537 Jul-10-2020, 06:51 PM
Last Post: bowlofred
  Python - change variable type during program execution ple 1 2,366 Apr-12-2020, 08:43 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020