In Python what is a global statement?

后端 未结 5 1449
情书的邮戳
情书的邮戳 2020-11-28 09:36

What is a global statement? And how is it used? I have read Python\'s official definition;
however, it doesn\'t make a lot of sense to me.

5条回答
  •  我在风中等你
    2020-11-28 09:55

    a = 1
    
    def f():
        a = 2 # doesn't affect global a, this new definition hides it in local scope
    
    a = 1
    
    def f():
        global a
        a = 2 # affects global a
    

提交回复
热议问题