Note on the use of global variables between modules in Python: python global variables

Source: Internet
Author: User

Note on the use of global variables between modules in Python: python global variables

Recently, I used Python to write code and used global variables between modules.

Search around the Internet and find that the common practice is to put the global variable into an independent module. Import the global variable module when using it.

However, some minor problems were found during actual use: when using the following code to import the global variable module, the global variables obtained by each module are the initial values.

from module import global_var

However, if "module name. global variable name" is used for access, it is normal:

import moduleprint module.global_var

To find out the source committee, I wrote a test program to carefully view the details:

 

 

1. import module

Global_var.py

GLOBAL_VAR = [1, 2, 3]

M1.py

import global_varimport m2print '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

import global_vardef append():    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_VARdef assign():    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

Running 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 the operation is to change the object itself (append), the operations in each module are targeted at the same object.

While the value assignment operation changes the object pointed to by the global variable, the global variable itself can still be correctly accessed within each module, which is exactly what I want.

 

 

2. from module import GLOBAL_VAR

Global_var.py

GLOBAL_VAR = [1, 2, 3]

M1.py

from global_var import GLOBAL_VARimport m2print '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

from global_var import GLOBAL_VARdef append():    global GLOBAL_VAR    print 'm2: before assiging:           ', id(GLOBAL_VAR), GLOBAL_VAR    GLOBAL_VAR.append('m2')    print 'm2: after assiging:            ', id(GLOBAL_VAR), GLOBAL_VARdef assign():    global GLOBAL_VAR    print 'm2: before assiging:           ', id(GLOBAL_VAR), GLOBAL_VAR    GLOBAL_VAR = ['m2']    print 'm2: after assiging:            ', id(GLOBAL_VAR), GLOBAL_VAR

Running 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 result is slightly different from the previous one.

The operation to change the object itself (append) is the same as in the previous example, and the global variable always points to the same object.

However, the assignment operation is strange. Note that in the red part, the global variable obtained by each module for the first time is an object with the same id. Even if the global variable has been assigned a value again in other modules, it still points to the original id in this module.

Obviously, this is not a global variable.

 


How does one Python module change the global variable value defined by another module?

Call get_a () in B ():
Global
A = 2
Return
 
How does python reference variables in methods of classes in other modules?

Cannot be implemented.
Cause:
Your tmp is a temporary variable in the fun function of class.
Therefore, it cannot be directly called by external users.
The function exists only when the fun function is executed and does not exist when the function ends.

For the scope of variables, refer:
[Arrangement] variable scope in Python)

If you want to be called, try:
Changed:

# A. py
Class ()
Tmp = '20140901'
Def fun ()
......

Then go:

Import
Print A. tmp

Note:
In this case, tmp is the global variable in class.

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.