0
empty_string = ''
string_1 = 'beta'

def sample(x):
    empty_string = empty_string + 'alpha'
    return x

为什么不:

empty_string = 'alpha'

我很困惑为什么empty_string我在函数中添加它之后仍然是空的

4

1 回答 1

5

使用global empty_stringinside 函数,那么只有你可以修改一个全局变量:

def sample(x):
    global empty_string
    empty_string = empty_string + 'alpha'
    return x
于 2012-09-24T21:18:21.467 回答