Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze Python Global variables in Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report