Dealing with global variables

We have already seen how to use global variables to share information between different functions within a module. We've seen how to define globals as top-level variables within a module, causing them to be initialized the first time the module is imported, and we have also seen how to use the global statement within a function to allow that function to access and change the value of a global variable.

In this section, we will build on this knowledge to learn how to share global variables between modules. When creating a package, you often need to define variables that can be accessed or changed by any module within that package. Sometimes, you also need to make a variable available to Python code outside your package. Let's take a look at how this can be done.

Create a new directory named globtest, and create an empty package initialization file inside this directory to make it a Python package. Then, create a file inside this directory named globals.py, and enter the following into this file:

language = None
currency = None

In this module, we have defined two global variables that we want to use in our package, and given each variable a default value of None. Let's now use these globals in another module.

Create another file in the globtest directory named test.py, and enter the following into this file:

from . import globals

def test():
    globals.language = "EN"
    globals.currency = "USD"
    print(globals.language, globals.currency)

To test your program, open a terminal or command-line window, use the cd command to move to the directory that contains your globtest package, and type python to start up the Python interactive interpreter. Then, try entering the following:

>>> from globtest import test
>>> test.test()
EN USD

As you can see, we have successfully set the value of the language and currency globals, which are stored in our globals module, and then retrieved these values again to print them out. Because we are storing these globals in a separate module, you can retrieve or change these globals anywhere within the current package or even in other code that imports your package. Using a separate module to hold your package's global variables is an excellent way of managing globals within a package.

There is, however, one thing to be aware of: for a global variable to be shared between modules, you must import the module that contains that global variable, not the variable itself. For example, the following won't work:

from .test import language

What this statement does is import a copy of the language variable into your current module's global namespace, not the original global. This means that the global variable won't be shared with other modules. For a variable to be shared between modules, you need to import the globals module, not the variables within it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.62.168