Global Variables for Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.