Python multiple files Use the same global variable definition and use method __python

Source: Internet
Author: User
Tags in python

Python is an object-oriented development language, the use of global variables in functions, generally should be described as global variables, only in the function of the described global variables can be used, the following describes the Python global variables related issues.

You should try to avoid using Python global variables. Different modules are free to access global variables, which can lead to the unpredictability of global variables. For global variables, if programmer a modifies the value of _a, this can cause errors in the program. Such errors are difficult to detect and correct.

Global variables reduce the commonality between functions or modules, and different functions or modules are dependent on global variables. Similarly, a global variable reduces the readability of the code, and the reader may not know that one of the variables invoked is a global variable. But at some point, Python global variables can solve problems that are difficult for local variables to solve. Things to be in Split. There are two flexible ways to use global variables in Python:

1. #gl. py

2. Gl_1 = ' Hello '

3. gl_2 = ' World '

4.

5. Use in other modules

6. #a. py

7. Import GL

8.

9. Def hello_world ()

Print Gl.gl_1, gl.gl_2

11.

#b. py

Import GL

14.

def fun1 ()

Gl.gl_1 = ' Hello '

gl.gl_2 = ' World '

Def modifyconstant ():

Global CONSTANT

Print CONSTANT

CONSTANT + 1

Return

23.

if __name__ = = ' __main__ ':

Modifyconstant ()

Num. Print CONSTANT

1 Declaration Law

The python global variable variable is declared at the beginning of the file, and the global variable is required in advance when the variable is used in a specific function, otherwise the system treats the variable as a local variable. CONSTANT = 0 (Capitalize global variables for easy identification)

2 Modular Method (recommended)

Recommended.

1. #gl. py

2. Gl_1 = ' Hello '

3. gl_2 = ' World '

4.

5. Use in other modules

6. #a. py

7. Import GL

8.

9. Def hello_world ()

Print Gl.gl_1, gl.gl_2

11.

#b. py

Import GL

14.

def fun1 ()

Gl.gl_1 = ' Hello '

gl.gl_2 = ' World '

Def modifyconstant ():

Global CONSTANT

Print CONSTANT

CONSTANT + 1

Return

23.

if __name__ = = ' __main__ ':

Modifyconstant ()

Num. Print CONSTANT

 

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.