为什么列表操作不返回结果列表? [英] Why don't list operations return the resulting list?
问题描述
我对导致这种情况的思考过程很感兴趣.对我来说,一个相对的新手,它似乎妨碍了,因为它阻止了列表处理的链接"(例如 mylist.reverse().append('a string')[:someLimit]
).我想可能是The Powers That Be"认为列表理解是一种更好的范式(有效的意见),因此不想鼓励其他方法 - 但阻止直觉方法似乎是不正当的,即使更好存在替代方案.
I'm interested in the thought process that led to this. To me, a relative newbie, it seems hampering, since it prevents "chaining" of list processing (e.g. mylist.reverse().append('a string')[:someLimit]
). I imagine it might be that "The Powers That Be" decided that list comprehension is a better paradigm (a valid opinion), and so didn't want to encourage other methods - but it seems perverse to prevent an intuitive method, even if better alternatives exist.
请注意,我不是在抱怨(我确定有一个合理的理由,我只是对它是什么感兴趣!),也不是在寻找解决方案(评论
Note that I'm not complaining (I'm sure there is a sensible reason, I'm just interested in what it is!), nor looking for a solution (the comments here were very instructive) - just looking for some context, and a deeper understanding of the language's design process.
推荐答案
Python 中的一般设计原则是针对 就地改变对象以返回 None
的函数.我不确定这是否是我选择的设计选择,但基本上是为了强调不返回新对象.
The general design principle in Python is for functions that mutate an object in-place to return None
. I'm not sure it would have been the design choice I'd have chosen, but it's basically to emphasise that a new object is not returned.
(GvR(我们的 Python BDFL)在此处说明了设计选择:http://mail.python.org/pipermail/python-dev/2003-October/038855.html)
(GvR's (our Python BDFL) states the design choice here: http://mail.python.org/pipermail/python-dev/2003-October/038855.html)
这篇关于为什么列表操作不返回结果列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!