Attention to the use of global variables between modules in Python

Source: Internet
Author: User

The recent writing of code in Python requires the use of global variables between modules.

Search around the web, it is common practice to put the global variables into a separate module, when used, import this global variable module.

But there are a few problems with the actual use: When importing a global variable module using the following code, the global variables that each module obtains are the initial values.

 from Import Global_var

However, it is normal to use the "module name. Global variable name" to access it:

Import Module Print Module.global_var

To find out what happened, I wrote a test program to look at the details:

1. Import Module

global_var.py

Global_var = [1, 2, 3]

m1.py

ImportGlobal_varImportm2Print 'M1:before Appending:', ID (global_var. Global_var), Global_var. Global_varglobal_var. Global_var.append ('M1')Print 'M1:after Appending:', ID (global_var. Global_var), Global_var. Global_varPrint 'M1:before Calling M2.append ():', ID (global_var. Global_var), Global_var. Global_varm2.append ()Print 'M1:after Calling M2.append ():', ID (global_var. Global_var), Global_var. Global_varPrint '-----------------'Print 'M1:before Assigning:', ID (global_var. Global_var), Global_var. Global_varglobal_var. Global_var= ['M1']Print 'M1:after Assigning:', ID (global_var. Global_var), Global_var. Global_varPrint 'M1:before Calling M2.assign ():', ID (global_var. Global_var), Global_var. Global_varm2.assign ()Print 'M1:after Calling M2.assign ():', ID (global_var. Global_var), Global_var. Global_var

m2.py

ImportGlobal_vardefappend ():Print 'm2:before assiging:', ID (global_var. Global_var), Global_var. Global_var Global_var. Global_var.append ('m2')    Print 'm2:after assiging:', ID (global_var. Global_var), Global_var. Global_vardefassign ():Print 'm2:before assiging:', ID (global_var. Global_var), Global_var. Global_var Global_var. Global_var= ['m2']    Print 'm2:after assiging:', ID (global_var. Global_var), Global_var. Global_var

Operation Result:

mac-pastgift:pytest pastgift$ python global_test_import/m1.pym1:before appending:4457308312[1, 2, 3]M1:after Appending:4457308312[1, 2, 3, ' M1 ']M1:before Calling M2.append ():4457308312[1, 2, 3, ' M1 ']m2:before assiging:4457308312[1, 2, 3, ' M1 ']m2:after assiging:4457308312[1, 2, 3, ' M1 ', ' m2 ']M1:after Calling M2.append ():4457308312[1, 2, 3, ' M1 ', ' m2 ']-----------------M1:before Assigning:4457308312[1, 2, 3, ' M1 ', ' m2 ']M1:after Assigning:4457444848[' M1 ']M1:before Calling M2.assign ():4457444848[' M1 ']m2:before assiging:4457444848[' M1 ']m2:after assiging:4457308312[' m2 ']M1:after Calling M2.assign ():4457308312[' m2 ']

In this way, if you change the operation of the object itself (append), the actions inside each module are for the same object.

The assignment operation, although the object that the global variable points to changes, but the global variable itself still within each module can be correctly accessed, this is exactly what I want to effect.

2. From module import Global_var

global_var.py

Global_var = [1, 2, 3]

m1.py

 fromGlobal_varImportGlobal_varImportm2Print 'M1:before Appending:', ID (global_var), Global_varglobal_var.append ('M1')Print 'M1:after Appending:', ID (global_var), Global_varPrint 'M1:before Calling M2.append ():', ID (global_var), Global_varm2.append ()Print 'M1:after Calling M2.append ():', ID (global_var), Global_varPrint '-----------------'Print 'M1:before Assigning:', ID (global_var), Global_varglobal_var= ['M1']Print 'M1:after Assigning:', ID (global_var), Global_varPrint 'M1:before Calling M2.assign ():', ID (global_var), global_varm2.assign ()Print 'M1:after Calling M2.assign ():', ID (global_var), Global_var

m2.py

 fromGlobal_varImportGlobal_vardefappend ():GlobalGlobal_varPrint 'm2:before assiging:', ID (global_var), Global_var global_var.append ('m2')    Print 'm2:after assiging:', ID (global_var), Global_vardefassign ():GlobalGlobal_varPrint 'm2:before assiging:', ID (global_var), Global_var Global_var= ['m2']    Print 'm2:after assiging:', ID (global_var), Global_var

Operation Result:

mac-pastgift:pytest pastgift$ python global_test_from_import/m1.pym1:before appending:4539998360[1, 2, 3]M1:after Appending:4539998360[1, 2, 3, ' M1 ']M1:before Calling M2.append ():4539998360[1, 2, 3, ' M1 ']m2:before assiging:4539998360[1, 2, 3, ' M1 ']m2:after assiging:4539998360[1, 2, 3, ' M1 ', ' m2 ']M1:after Calling M2.append ():4539998360[1, 2, 3, ' M1 ', ' m2 ']-----------------M1:before Assigning:4539998360 [1, 2, 3, ' M1 ', ' m2 ']M1:after Assigning:4540135112[' M1 ']M1:before Calling M2.assign ():4540135112[' M1 ']m2:before assiging:4539998360 [1, 2, 3, ' M1 ', ' m2 ']m2:after assiging:4540135040[' m2 ']M1:after Calling M2.assign ():4540135112[' M1 ']

This time, the running results are slightly different from the last time.

The action of changing the object itself (append) is the same as the previous example, where the global variable always points to the same object.

But the assignment is a bit odd. Note that the "global variable" that each module obtains for the first time is an object of the same ID. Even though this "global variable" has been re-assigned in other modules, it still points to the most original ID in this module.

Obviously, this is not a "global variable".

Attention to the use of global variables between modules in 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.