The Python 3 implementation defines global variables across modules and uses

Source: Internet
Author: User

While some books always say avoiding global variables, it is often the most reliable way to define a global variable in the face of changing actual requirements, but you must avoid variable name overrides.

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

We know that Python can be used directly when using variables, x=[], y=2,z= "123", without first defining (Var x; var y=2;var z= ' 222 '), so that external variables cannot be manipulated inside the function, because it will always assume that you are defining a new variable and assigning values, but global can solve the problem.

basic usage of global :

x = 6def  func ():    Global  x #定义外部的x    = 1func ()print  (x )# Output 1

this time, even if you are using another PY file again Global x is inaccessible because there is not a variable called X in this py module, and the error is undefined.

So what do we do?

Learn from the Global keyword idea, since in a file can be effective, then we specifically for the global variables to define a "global variable Management module" just fine.

Specific code:

#-*-Coding:utf-8-*-
def_init ():#初始化Global_global_dict _global_dict= {}
defSet_value (key,value):"""define a global variable"""_global_dict[key]=value
defGet_value (key,defvalue=None):
"" "gets a global variable that does not exist and returns the default value" ""Try: return_global_dict[key]exceptKeyerror:returnDefvalue

Believe that if you understand that you should know the idea, using global individual files globally, so that you can define a global variable in a file, and then this single file global variable can hold multiple files of common global variables

Operation, the key to the value of the method operation, I believe we all understand.

# -*-coding:utf-8-*-  from Import Golgol._init () # must be initialized in the main module first (only one time is required in the main module) # define cross-module global variables gol.set_value ('CODE', ' UTF-8 ') gol.set_value (  'PORT', Gol.set_value ('HOST', ' 127.0.0.1 ')

Then any other file just needs to be imported to use:

# -*-coding:utf-8-*-  from Import Gol # no more initialization required. Root = Gol.get_value ('root',= Gol.get_value ('CODE  ')

It's just so simple ~

This enables the use of global variables across files;

And there is a simple but powerful global variable manager, you can make up your own, implement some such as global variables prohibit direct modification, prohibit the modification of some only readable global variables and so on.

Thank you for your patience in reading, if you have any help I will be very honored, if there are mistakes in the place to correct.

The Python 3 implementation defines global variables across modules and uses

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.