Python multi-module file sharing variables

Source: Internet
Author: User

The mechanism of the Python import package is that the import incoming and default system module is placed in the Sys.module dictionary.
Multiple py file import again, will go to sys.module inside check whether already import, if already import, no longer repeat import, otherwise import come in

===

The namespaces for import Glovar and from Comon import Glovar are the same, and key is all Glovar

From Common.glovar import x is different, so from import in test.py, which is equivalent to writing a line of code x = 1 in the test.py file,
At this point, X is the variable in the test's own namespace. So x is only effective in test.py, boring How to modify X, can not affect the X in Glovar

#换种说法from glovar import x#等同于improt glovarx= glovar.x#你调用x的时候,就是自己的命名空间中的x#当你执行x=2时,完全影响不到glovar.x

Here's an explanation.

So, if you need to share a variable, do not use the From file import x form, but instead use the import file, then you can use the file.x, and then file.x= ' ABC ' can be modified. This way, the global variables can be shared. That is, keep a separate namespace so that Python does not import again, allowing for sharing.



Coreki
Links: http://www.jianshu.com/p/4bb742d7d672
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Python multi-module file sharing variables

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.