Skip to main content
 首页 » 编程设计

python之如何从 Python 中的内部函数更改外部函数局部变量

2025年05月04日16cyq1162

<分区>

Possible Duplicate:
Modify the function variables frominner function in python

假设我有这个 python 代码

def f(): 
    x=2 
    def y(): 
        x+=3 
    y() 

这引发了:

UnboundLocalError: local variable 'x' referenced before assignment 

那么,如何从内部函数“修改”局部变量'x'?在内部函数中将 x 定义为全局变量也会引发错误。