def increment(): global num num += 1</sxh>
from test import increment, num global num increment() print(num) test.py global num num = 0 def increment(): global num num += 1
from test import increment, num print(‘before increment num in main.py, num address is {}’.format(id(num))) increment() print(‘final num value {} and address {}’.format(num, id(num)))
global num num = 0 print(‘import test.py, num address is {}’.format(id(num))) def increment(): global num print(‘call by main.py, before increment, num address is {}’.format(id(num))) num += 1 print(‘call by main.py, after increment , num address is {}’.format(id(num)))
before increment num in main.py, num address is 140467528860864 call by main.py, before increment, num address is 140467528860864 call by main.py, after increment , num address is 140467528860896 final num value 0 and address 140467528860864
def initialize(): global num num = 1
import globals import test if __name__ == “__main__”: globals.initialize() print( globals.num ) # print the initial value test.increment() print( globals.num ) # print the value after being modified within test.py
import globals def increment(): globals.num += 1
1 2