在Python
3.x中,可以使用
nonlocal关键字:
def outer(): string = "" def inner(): nonlocal string string = "String was changed by a nested function!" inner() return string
在Python 2.x中,您可以使用带有单个元素的列表并覆盖该单个元素:
def outer(): string = [""] def inner(): string[0] = "String was changed by a nested function!" inner() return string[0]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)