Get the App
SLTechnology News&Howtos  ›  Development  › 

How to analyze Python Global variables in Python

Shulou Source: shulou.com Published: 2022-06-02 22:38:21 09月18日 Update

How to analyze the Python global variables in Python, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Python is an object-oriented development language, the use of global variables in the function, generally should be global variables description, only in the function after the description of global variables can be used, the following introduction of Python global variables related issues.

You should avoid using Python global variables as much as possible. Different modules can freely access global variables, which may lead to the unpredictability of global variables. For global variables, if programmer A modifies the value of _ a, it may lead to errors in the program. This kind of mistake is difficult to find and correct.

Global variables reduce the versatility between functions or modules, and different functions or modules depend on global variables. Similarly, global variables reduce the readability of the code, and the reader may not know that a variable called is a global variable. But sometimes, Python global variables can solve the problems that local variables are difficult to solve. Things should be divided into two. There are two flexible uses of global variables in python:

# gl.py gl_1 = 'hello' gl_2 =' world' uses # a.py import gl def hello_world () print gl.gl_1 in other modules Gl.gl_2 # b.py import gl def fun1 () gl.gl_1 = 'Hello' gl.gl_2 =' World' def modifyConstant (): global CONSTANT print CONSTANT CONSTANT + = 1 return if _ _ name__ ='_ _ main__': modifyConstant () print CONSTANT

1 declaration method

Declare the Python global variable variable at the beginning of the file. When you use this variable in a specific function, you need to declare global variable in advance, otherwise the system treats the variable as a local variable. CONSTANT = 0 (capitalize global variables for easy identification)

2 Modular method (recommended)

Oh, recommend!

# gl.py gl_1 = 'hello' gl_2 =' world' uses # a.py import gl def hello_world () print gl.gl_1 in other modules Gl.gl_2 # b.py import gl def fun1 () gl.gl_1 = 'Hello' gl.gl_2 =' World' def modifyConstant (): global CONSTANT print CONSTANT CONSTANT + = 1 return if _ _ name__ ='_ _ main__': modifyConstant () print CONSTANT is helpful to you after reading the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Variable global module function different local program error problem help recommendation clarity freedom split in two between things code content internal scriptures only Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Apple Shulou Technology MySQL Redmi