小编yaf*_*nee的帖子

Python:list.extend和list .__ iadd__之间的区别

我认为list.extend列表中的"+ ="基本上做同样的事情 - 扩展列表而不创建新列表.

我希望下面的代码可以打印,[42, 43, 44, 45, 46]但我得到了UnboundLocalError: local variable 'x' referenced before assignment

为什么我收到此错误?区别在哪里?

def f():
    x.extend([43, 44])
def g():
    x += ([45, 46])
x = [42]
f()
g()
print x
Run Code Online (Sandbox Code Playgroud)

我在python2.7.3和python3.4.0中尝试过这个.

python

3
推荐指数
1
解决办法
497
查看次数

标签 统计

python ×1