程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Simple use of Python global variable keyword Global

編輯:Python

Catalog

brief introduction :

Case study 1: Global cannot use local variables .

Case study 2: Global variables , Any range can be used .

Case study 3: A local variable defined in a function

Case study 4: Between functions global The role of keywords

Case study 5: In different file modules global, Be careful test6, test7 For different files .

Case study 6: In different file modules global, Be careful test6, test7 For different files .

attach : use global Declaration of multiple variables needs to be separated by commas

Conclusion :

brief introduction :

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 .

Case study 1: Global cannot use local variables .# -*- 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 Case study 2: Global variables , Any range can be used .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) Case study 3: A local variable defined in a function 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() Case study 4: Between functions global The role of keywords 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() Case study 5: In different file modules global, Be careful test6, test7 For different files .# 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' Case study 6: In different file modules global, Be careful test6, test7 For different files .# 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 attach : use global Declaration of multiple variables needs to be separated by commas 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 Conclusion :

1、 Import packages only ,global The defined global variable is not added to globals Inside .

2、 perform global The corresponding function ,global The defined function will store the corresponding variables , Other functions do not store .

This is about Python Global variable keyword global This is the end of the article on the simple use of , More about Python Global variable keyword global Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved