PEP 322:反向迭代(修改,请评论) [英] PEP 322: Reverse Iteration (REVISED, please comment)

查看:47
本文介绍了PEP 322:反向迭代(修改,请评论)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据您的广泛反馈,PEP 322已经完全修改。

响应非常积极,但几乎所有人都喜欢

有一个函数而不是多个对象方法。更新的

提案位于:

www.python.org/peps/pep-0322.html

简而言之,它提出了一个内置函数,可以大大简化反向/>
迭代。核心概念是清晰度来自于向前指定一个

序列,然后说:inreverse():


for elem in inreverse(seqn):

。 。 。


与seqn [:: - 1]不同,这会生成一个快速迭代器,而不是完全反转的

副本。


与Guido的讨论清楚地表明,inreverse()不会延长

来涵盖所有的迭代。该提案涉及简单性,表达和

性能。因此,将一般的

迭代,运行完成,将数据保存在内存中,然后对数据中的数据进行迭代将会适得其反。反向。

Raymond Hettinger

Based on your extensive feedback, PEP 322 has been completely revised.
The response was strongly positive, but almost everyone preferred
having a function instead of multiple object methods. The updated
proposal is at:

www.python.org/peps/pep-0322.html

In a nutshell, it proposes a builtin function that greatly simplifies reverse
iteration. The core concept is that clarity comes from specifying a
sequence in a forward direction and then saying, "inreverse()":

for elem in inreverse(seqn):
. . .

Unlike seqn[::-1], this produces a fast iterator instead of a full reversed
copy.

Discussions with Guido made it clear that inreverse() will not be extended
to cover all iterables. The proposal is about simplicity, expression, and
performance. As such, it would be counter-productive to take in a general
iterable, run it to completion, save the data in memory, and then iterate
over the data in reverse.
Raymond Hettinger

推荐答案

Raymond Hettinger写道:
Raymond Hettinger wrote:
<与Guido的讨论清楚地表明,inreverse()不会被扩展以涵盖所有的迭代。该提案涉及简单性,表达和性能。因此,采用一般的可迭代,运行完成,将数据保存在内存中,然后反向迭代数据将会适得其反。

Discussions with Guido made it clear that inreverse() will not be extended
to cover all iterables. The proposal is about simplicity, expression, and
performance. As such, it would be counter-productive to take in a general
iterable, run it to completion, save the data in memory, and then iterate
over the data in reverse.




当然可以很容易地明确地完成:


#使一些迭代不适合反向

a = iter(" ; Hello")


#创建一个可以明确表示的列表

for in inreverse(list(a)):

打印我

一如既往,明确比隐含更好;-)


也许类似的东西应该进入文档...

- Werner



Which of course can easily be done explicitely:

# Make some iterable unsuitable for inreverse
a = iter("Hello")

# Create a list that can be in''reversed explicitely
for i in inreverse(list(a)):
print i
As always, explicit is better than implicit ;-)

Maybe something alike should go into the docs...
- Werner





是否考虑将该功能命名为反向?


这对我来说非常自然:


for elem in reverse(seq):

打印elem

另外如果你在函数调用中使用它对我来说很好看:


x = dostuff(反向(seq),a ,b)

另一种可能性是使用静态方法:


for elem in iter.reversed(sed):

print elem


(在这种情况下我会使用属性而不是动词,因为

生成的iter(ator)将是reverse_d_)。


静态方法方法会清楚地记录

结果是一个迭代器(没有提出其他名称

真的恕我直言)

一般情况下,我在PEP上+1,

问候


Werner

Hi,

was it considered to name the function just "reverse"?

This would read very naturally to me:

for elem in reverse(seq):
print elem
Also if you use it in function calls it looks good to me:

x = dostuff(reverse(seq), a, b)
Another possibility would be to have a static method for iter:

for elem in iter.reversed(sed):
print elem

(in that case I''d use an attribute rather than a verb, because
the generated iter(ator) will be reverse_d_).

The static method approach would clearly document that the
result is an iterator (which none of the other names proposed
really does IMHO)
In general, I''m +1 on the PEP
regards

Werner


pyvis@rcn.com (雷蒙德Hettinger写道:
py****@rcn.com (Raymond Hettinger) writes:
简而言之,它提出了一个内置函数,可以大大简化反向迭代。核心概念是清晰度来自于向前指定一个序列,然后在inreverse(seqn)中对elem说inreverse():


。 。 。


我必须说,我真的不喜欢这个名字。特别是,

重复在...中在inreverse ...中让我感到很难看。

与Guido的讨论清楚地表明,inreverse()不会被延伸,以涵盖所有的迭代。该提案涉及简单性,表达和性能。因此,采用一般的可迭代,运行完成,将数据保存在内存中,然后反向迭代数据将会适得其反。
In a nutshell, it proposes a builtin function that greatly simplifies reverse
iteration. The core concept is that clarity comes from specifying a
sequence in a forward direction and then saying, "inreverse()":

for elem in inreverse(seqn):
. . .
I''ve got to say, I really don''t like the name. Particularly, the
repeated "in" in "in inreverse..." strikes me as ugly.
Discussions with Guido made it clear that inreverse() will not be extended
to cover all iterables. The proposal is about simplicity, expression, and
performance. As such, it would be counter-productive to take in a general
iterable, run it to completion, save the data in memory, and then iterate
over the data in reverse.




我喜欢这个提案,它简单而富有表现力。但请更改

的名字!我假设有一个很好的理由为什么没有选择明显的反向()

。在PEP中的替代方案中,向后()并不是美元b $ b看起来很好看(但它比反向更好!!!)和ireverse

没关系,但让我觉得它应该在itertools中(或者所有的b / b $ b $应该是内置的)。其他一些可能性:


rev(seq) - 可能太简陋

逆转(seq) - 形容词而不是动词,但读得很好


像往常一样,语义是完全可以接受的,但我们将在名称上有一个很好的

长的争论:-)


Paul。

-

此签名故意留空



I like the proposal, it''s simple and expressive. But please, change
the name! I assume there''s a good reason why the obvious "reverse()"
wasn''t chosen. Of the alternatives in the PEP, backwards() doesn''t
seem to read nicely (but it''s better than inreverse!!!), and ireverse
is OK, but makes me think that it should be in itertools (or all the
itertools should be builtin). Some other possibilities:

rev(seq) - probably too abbreviated
reversed(seq) - adjective rather than verb, but reads well

As usual, the semantics is perfectly acceptable, but we''ll have a nice
long argument over the name :-)

Paul.
--
This signature intentionally left blank


这篇关于PEP 322:反向迭代(修改,请评论)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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