Conclusion: Python's global variables are only valid in this file.
Defines a file for a global variable g.py
1 #define some global variable2 3A = 14B = 25C = 36 7 defFuck (A=0, b=0, c=0):8 GlobalA, B, C9A =aTenB =b OneC =C A - defFuck2 (): - GlobalA, B, C the Print 'In fuck2, A =%d, B =%d, C =%d'% (A, B, C)
Files that use global variables use_g.py
fromGImport*defshit ():GlobalA, B, CPrint 'before, A =%d, B =%d, C =%d'%(A, B, C) Fuck () fuck2 ( )Print 'After , A =%d, B =%d, C =%d'%(A, B, C)if __name__=='__main__': Shit ()
The printing results are as follows
1 2 3 000 # ===> visible in the same file, changes to global variables can be passed. 1 2 3 # ===> visible across files, global variables cannot be passed.
# workaround 1: Use return values to bring back.
# workaround 2: Produce a script for global variables to write intermediate files, such as writing a cpickle file, or a database file, using a global variable script to read the intermediate file
Finish.
Global Variables for Python