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

How to use Python global variable keyword Global

編輯:Python

Python Global variable keyword global How do you use it?

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 .

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

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 .


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