How does Python modify variables across files?

I want to modify the variable of module an in module b, and then use the modified variable in module a.

a module code:

import b

flag = False

def set_flag(is_ok):
    global flag
    flag = is_ok
    
def test():
    global flag
    -sharp ba
    b.change_var()
    
    -sharp 
    if flag:
        print("success")
    else:
        print("failure")
    
    print(flag)

if __name__ == "__main__":
    test()

b module code:

import a

def change_var():
    a.set_flag(True)

run result:

failure
False

how to solve this?

Jul.13,2021

this won't work, you an import bjol b import a, circular reference.
python when referencing a file, it will run the file once, an import b equals to run b first, but if b import a, it will run an again, forming an endless loop.

I don't think the answer you want to know will solve your real needs.

I can't guess the real purpose of this method you want to achieve. But this function should be designed as follows:
b implements a method that accepts the old state of a variable and returns the new state of a variable. Call this method in
a, pass in the old state, and re-copy the new value returned to the variable.


The

a module is changed to the following form. Import other modules only when you are the main program

if __name__ == '__main__':
    import b
    test()
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea5e5b-4f262.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea5e5b-4f262.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?