Python global/local variables

后端 未结 6 1455
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 14:58

Why does this code work:

var = 0

def func(num):
    print num
    var = 1
    if num != 0:
        func(num-1)

func(10)

but this one give

6条回答
  •  庸人自扰
    2020-12-01 15:38

    You can read a global without declaring it global. But to write a global, you need to declare it global.

提交回复
热议问题