0

This question may have a simple answer. I have a near-trivial module0.py:

a = 1
b = a + 3

I import this module into a script and redefine module0's global constant a:

import module0 as m0
m0.a = 2
print(m0.b)

However, when I run this code, it prints 4 rather than 5. Is there a way to import module0 and change the value of m0.a so that all variables such as m0.b in the global namespace of the imported module are (in some sense) automatically updated with the new value of m0.a?

1
  • Thanks for these comments. After reflection, this does feel like a pythonic solution. I added a function set_dependent_global_vars() to module0.py which defines and declares as global all variables like b. Then I call this function immediately after m0.a = 2 in the script and everything works as hoped. Commented Apr 27 at 22:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.