Python列表+ =可迭代的行为是否记录在任何地方? [英] Is the behaviour of Python's list += iterable documented anywhere?

查看:258
本文介绍了Python列表+ =可迭代的行为是否记录在任何地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在Python中,list += x适用于任何可迭代的x:

It would appear that in Python, list += x works for any iterable x:

In [6]: l = []

In [7]: l += [1]

In [8]: l += (2, 3)

In [9]: l += xrange(5)

In [10]: l
Out[10]: [1, 2, 3, 0, 1, 2, 3, 4]

此行为在任何地方都有记录吗?

Is this behaviour documented anywhere?

为了与list + x进行对比,后者仅在x也是list时才起作用.这在文档.

To contrast this with list + x, the latter only works if x is also a list. This is spelled out in the documentation.

推荐答案

来自 Guido van Rossum :

它与.extend()的工作方式相同,除了它还会返回self.一世 找不到说明此问题的文档. :-(

It works the same way as .extend() except that it also returns self. I can't find docs explaining this. :-(

以下是来自 listobject.c 的相关源代码:

Here is the relevant source code taken from listobject.c:

list_inplace_concat(PyListObject *self, PyObject *other)
{
     PyObject *result;

     result = listextend(self, other);
     if (result == NULL)
         return result;
     Py_DECREF(result);
     Py_INCREF(self);
     return (PyObject *)self;
}

我提出了一个错误报告来修复文档: http://bugs.python.org/issue16701

I've raised a bug report to have the documentation fixed: http://bugs.python.org/issue16701

这篇关于Python列表+ =可迭代的行为是否记录在任何地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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