This article “Python Global variable keyword global How do you use it? ” Most people don't quite understand the knowledge points of the article , So I made up the following summary for you , Detailed content , The steps are clear , It has certain reference value , I hope you can gain something after reading this article , Let's take a look at this article “Python Global variable keyword global How do you use it? ” Article bar .
1、global yes Python Global variable keywords in .
2、 Global variables are a kind of programming terminology , From variables .
3、 Variables are divided into local and global , Local variables can also be called internal variables .
4、 Variables created by an object or function are usually local variables , Can only be quoted internally , It cannot be referenced by other objects or functions .
5、 Global variables can be created by some object function , It can also be created anywhere in this program . Global variables can be referenced by all objects or functions in this program .
6、global Keyword is used to make a local variable a global variable .
# -*- coding: utf-8 -*-def test1(): # local variable local local_var = "a" print(local_var)# Global cannot use local variables , Only the corresponding local scope is available # print(local_var) # NameError: name 'local_var' is not defined
global_var = 1def test2(): # Use global variables in functions print(global_var + 1) def inner(): # Use global variables within nested functions print(global_var + 2) return global_var + 3 # Global variables are used in the return value # Use global variables outside the function .print(global_var)
def test3(): # Variables within a function , But for subordinate functions, they are global variables , Externally, it is a local variable func_var = 1 def inner(): print(func_var) return func_var return inner()test3()
def test4(): # global Key role global func_var func_var = 2 # call test5 You can print func_var, Get rid of global Will report a mistake . test5() print(test5.__globals__)def test5(): print(func_var)test4()
# a.pydef test6(): # global Key role global func_var func_var = 3 # b.pyfrom a import test6def test7(): print(test6.__globals__["func_var"])# Don't do it first test6 In this case, an exception is thrown .KeyError: 'func_var'test7() # KeyError: 'func_var'
# a.pydef test6(): # global Key role global func_var func_var = 4 # b.pyfrom a import test6def test7(): print(test6.__globals__["func_var"])# Execute first test6 Under the circumstances ,test have access to func_vartest6()test7() # 4
num = 0def cc(): global count,num count = count+1 num = num+2 print(count,num)cc()3 2# You can use... In a function global Declare the ability to modify global variables numOut[24]: 2# countOut[25]: 3 When using global variables , You can also use class variables instead of class C: count = 3def cc(): count = C.count+1 print(count)cc()4
That's about “Python Global variable keyword global How do you use it? ” The content of this article , I believe we all have a certain understanding , I hope the content shared by Xiaobian will be helpful to you , If you want to know more about it , Please pay attention to the Yisu cloud industry information channel .