Python扩展了一个空列表错误? [英] Python extend with an empty list bug?

查看:65
本文介绍了Python扩展了一个空列表错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么python 2.5.2具有以下行为

Why does python 2.5.2 have the following behavior

>>>[2].extend([]) == [2]
False

>>> [2].extend([]) == None
True

$ python --version
Python 2.5.2

我认为我对这里的内容不了解,但直觉上我认为[2] .extend([])应该会产生[2]

I assume I'm not understanding something here, but intuitively I'd think that [2].extend([]) should yield [2]

推荐答案

扩展是list的一种方法,它对其进行了修改,但不返回self(而是返回None).如果需要将修改后的值用作表达式值,请使用+,如[2]+[]所示.

Extend is a method of list, which modifies it but doesn't return self (returning None instead). If you need the modified value as the expression value, use +, as in [2]+[].

这篇关于Python扩展了一个空列表错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆