Python 全局未按预期运行



下面的打印出"None"而不是"whatever"。我不明白为什么。

dirr = None
def method():
    global dirr # you have to declare that you'll use global variable 'dirr'
    dirr = "whatever"
print `dirr`

你应该调用'method'一次。

dirr = None
def method():
    global dirr # you have to declare that you'll use global variable 'dirr'
    dirr = "whatever"
method()
print (dirr)

结果是:

whatever

相关内容

  • 没有找到相关文章

最新更新