Python全球表现不尽如人意

问题描述 投票:0回答:1

以下打印出“无”而不是“无论什么”。我不明白为什么。

dirr = None

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

print `dirr`
python global-variables global
1个回答
1
投票

你应该一次叫'方法'。

dirr = None

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

method()
print (dirr)

结果是:

whatever
© www.soinside.com 2019 - 2024. All rights reserved.