Python global global variable function Detailed

This article explain the global use of global variables functions, as well as global role. global scope of all global variables can be accessed in a script, the usage is very convenient, I hope this article to be helpful
in a global statement of action

When writing a program, if the outside want is a function of the variable value reassignments, and this variable will act on the many functions, you need to tell python the scope of the variable is a global variable. At this point it can become a global statement this task, that is to say the absence of a global statement can not modify global variables.

When using python function, often encounter problems defined parameters. If you do not declare global variables, will complain

count = 1
def cc():
 count = count+1
cc()
Traceback (most recent call last):
File "<ipython-input-17-f6b58c567c1a>", line 1, in <module>
 cc()
File "<ipython-input-16-aab94f1185b9>", line 2, in cc
 count = count+1
UnboundLocalError: local variable 'count' referenced before assignment

This means that count as local variables, have not been assigned before using it. You can not use global variables directly in the function.

num = 1
id(num)
Out[31]: 1886744032
def cc():
 num = 2
 print(id(num))
 print(num)
cc()
1886744064
2

It can be seen inside the function num is a local variable, there is any way to use global variables within the function? According to the official document, you can use the global statement:

1.The global statement is a declaration which holds for the entire current code block. It means that the
2.listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without global.

This means that global statement can declare one or more variables as global variables. This statement is only valid in the current code block. In addition, no way to access the global variables. So add a global statement in the function:

def cc():
 global count
 count = count+1
 print(count)
cc()
2

Need separated by commas with global declare multiple variables:

num = 0
def cc():
 global count,num
 count = count+1
 num = num+2
 print(count,num)
cc()
3 2
# 可以函数中的global声明能够修改全局变量
num
Out[24]: 2
# 
count
Out[25]: 3
在使用全局变量的场合,也可用类变量代替
class C:
 count = 3
def cc():
 count = C.count+1
 print(count)
cc()
4

If want to call a global variable in the function, which requires a global statement. After calling a global variable, the value of global variables also will be likely to change, and if count = count + 1 when such re-defined, otherwise global variables just be called. You can also call the variable by class way to achieve the effect of global variables
recommend our learning Python buckle qun: 774711191, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining, etc. [PDF, actual source code], zero-based projects to combat data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering

The global effect equivalent to pass parameters in variable external function declarations, if you want to use the function, it is used to declare the global variable, this is equivalent to the passed in a variable, you can refer to the variable a

Summary
That's all for this article, I hope the content of this article has some reference value of learning for everyone to learn or work

Published 35 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/haoxun02/article/details/104270732