[Python] Python: change global variable from within another file

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

  1. Stack

    Stack Membro Participativo

    In my main class I use a global variable ADDRESS that is defined in another file called config.py. I want to change the value of ADDRESS from within my main class, by calling a function in config.py.

    In my main class I have:

    from config import ADDRESS, change_address
    change_address("192.168.10.100")
    print("new address " + ADDRESS)


    In my config file I have the following:

    ADDRESS = "0.0.0.0"

    def change_address(address):
    global ADDRESS
    ADDRESS = address
    print("changed address to: " + ADDRESS)


    The print statement in my config file correctly prints the new address of 192.168.10.100. However, the print statement in my main class prints 0.0.0.0. What am I missing here?

    Continue reading...

Compartilhe esta Página