Python 3 Implementation Defining Cross Module Global Variables and Using Tutorials

  • 2021-07-10 20:19:58
  • OfStack

Although some books always say to avoid using global variables, it is often the most reliable method to define a global variable in the changing actual requirements, but it is necessary to avoid overwriting variable names.

The global keyword in Python can define 1 variable as a global variable, but this is limited to calling global variables in 1 module (py file):

We know that Python can use variables directly, x = [], y = 2, z = "123" without defining them first (var x; var y = 2; var z = '222'). In this way, it is impossible to manipulate external variables inside the function, because it always thinks that you are defining a new variable and assigning a value, but global can solve this problem.

Basic usage of global:


x = 6
def func():
  global x # Define external x
  x = 1
func()
print (x)
# Output 1

At this time, even if you use global x again in another py file, it will not be accessible, because there is not a variable called x in this py module, so an error will be reported undefined.

So what do we do?

Using the idea of global keyword for reference, since it can take effect in a file, we will define a "global variable management module" for global variables

Specific code:


# -*- coding: utf-8 -*-
def _init():# Initialization 
  global _global_dict
  _global_dict = {}
def set_value(key,value):
  """  Definition 1 Global variables  """
  _global_dict[key] = value
def get_value(key,defValue=None):
    """  Obtain 1 Global variables , Return the default value if it does not exist  """
  try:
    return _global_dict[key]
  except KeyError:
    return defValue

Believe that if you understand should know the idea, the use of global individual file global, so that you can define the global variables in a file, and then the global variables of a single file can save the common global variables of multiple files

When operating, I believe everyone understands the method of Key versus Value.


# -*- coding:utf-8 -*-
from YourPage import gol
gol._init()# It must be initialized in the main module (only in the Main Module requirements 1 Only once) 
# Defining cross-module global variables 
gol.set_value('CODE','UTF-8')
gol.set_value('PORT',80)
gol.set_value('HOST','127.0.0.1')

Then any other files just need to be imported to use:


# -*- coding: utf-8 -*-
from YourPage import gol
# No more initialization required 
ROOT = gol.get_value('ROOT',80)
CODE = gol.get_value('CODE')


It's as simple as that ~

In this way, the use of global variables across files can be realized;

And there is a simple but powerful global variable manager, you can embellish their own, such as the implementation of a global variable to prohibit direct modification, prohibit modification of some readable global variables and so on.

Summarize

Above is this site to introduce you to Python 3 to achieve the definition of cross-module global variables and use tutorials, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: