[Python] How do I change a global constant of a loaded module so that other global variables...

Discussão em 'Python' iniciado por Stack, Abril 27, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página