PEP 322:反向迭代(第二次修订,请发表评论) [英] PEP 322: Reverse Iteration (second revision, please comment)

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

问题描述

根据comp.lang.python上的反馈,pep已经更新了



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

主要变化是:


* reversed()首选ireverse()作为最佳名称。


*示例实现现在清楚地显示了一个检查对于自定义

反向方法和防止应用于映射。


*为枚举.__ reverse__添加了示例输出以显示

自定义反向方法如何工作


*解释了为什么函数被建议作为内置函数以及为什么附件

到另一个模块或类型对象是没有进一步考虑。


*扩展了对现实世界用例的评论,以显示

替换代码的样子。

请继续提出您的想法。我特别感兴趣的是人们在检查他们自己的代码以验证新功能

增加清晰度并提高性能的人们。


另外,请看看revrange()替代方案,看看你是否喜欢b $ b更喜欢它。

Raymond Hettinger

< br>

Based on the feedback here on comp.lang.python, the pep has been
updated:

www.python.org/peps/pep-0322.html
The key changes are:

* reversed() is being preferred to ireverse() as the best name.

* the sample implementation now clearly shows a check for a custom
reverse method and a guard against being applied to a mapping.

* added sample output for enumerate.__reversed__ to show
how a custom reverse method would work

* explained why the function is proposed as a builtin and why attachment
to another module or type object is not being considered further.

* expanded comments on the real world use cases to show what the
replacement code would look like.
Please continue to contribute your thoughts. I''m especially interested
in people examining their own code to verify that the new function
adds clarity and improves performance.

Also, please take a look at the revrange() alternative to see if you
prefer it or not.
Raymond Hettinger


推荐答案

Raymond Hettinger写道:
Raymond Hettinger wrote:
* reversed()首选ireverse()为最佳名字。


+1


即使反向因为动词与zip更加一致。并且

" enumerate",可以假设改变传递的序列为arg

(听起来像命令)


所以我同意反转是最好的选择IMHO

另外,请看看revrange()替代方案,看看你是否喜欢它。
* reversed() is being preferred to ireverse() as the best name.

+1

even though "reverse" as a verb would be more consistent with "zip" and
"enumerate", it could be assumed to change the sequence passed as arg
(it sounds like a command)

So I agree that "reversed" is the best alternative IMHO

Also, please take a look at the revrange() alternative to see if you
prefer it or not.




就我个人而言,我认为编写xrange(len(seq)-1,-1,-1)要比

更容易,以便记住一个简单的函数名称。 br />

此外,在极少数情况下,我确实需要索引。

在这种情况下,枚举主要是我真正想要的。


我不记得曾经有过反向枚举的需要...


恕我直言:恕我直言:


for seq中的商品:

打印商品


比:


for我在xrange(len(seq)):

打印seq [i]


因为前者传达了意图(为所有项目做了一些事情

顺序)。


这是我在e工作时真正想念的事情之一。 G。 C

我是-0-一般是旋转,-1是反转的替代品。


对我而言,它增加了复杂性而不是帮助...

最好的问候


Werner



Personally I think it''s easier to write xrange(len(seq)-1,-1,-1) than
to remember one more function name for something that trivial.

In addition, there are rare cases where I did ever need the index.
And in that cases, enumerate is mostly what i *really* wanted.

I cannot remember to ever having had a need for reverse enumerate...

It IMHO much more beatiful to say:

for item in seq:
print item

than:

for i in xrange(len(seq)):
print seq[i]

because the former communicates the intent (do something for all items
in the sequence).

This is one of the things I really miss when doing work in e. g. C
I''m -0 on revrange in general and -1 as a replacement for "reversed".

To me it rather adds complexity than helps...
best regards

Werner


Raymond Hettinger写道:

...
Raymond Hettinger wrote:
...
*示例实现现在清楚地显示了对自定义
反向方法的检查以及防止应用于映射。


语法错误:''返回''带有生成器内的参数


所以假设检查必须在PEP中重新编码为:


尝试:

customiter = x .__ reversed__

除了AttributeError:

pass

else:

for i in customiter():

yield i

return


是的,如果返回内部参数

generator,有时它会很好。自动扩展为for ...:yield / return。


另外,请查看revrange()替代方案,看看你是否喜欢它或者没有。
* the sample implementation now clearly shows a check for a custom
reverse method and a guard against being applied to a mapping.
SyntaxError: ''return'' with argument inside generator

So presumably said check must be recoded in the PEP as:

try:
customiter = x.__reversed__
except AttributeError:
pass
else:
for i in customiter():
yield i
return

Yeah, sometimes it WOULD be nice if "return with argument inside
generator" automagically expanded to "for ...: yield / return".

Also, please take a look at the revrange() alternative to see if you
prefer it or not.




它可以更直接地处理大约一半的用例,但

直接减少另一半,所以''大致洗了一下。但

具有revrange返回列表只会使该范围现在变成的小疣

永久存在,并且不会返回列表(相反

迭代器会违反最小惊喜原则。


内置迭代器irange - 带有可选的reverse =参数,

就像你最近添加到list.sort中的非常有用的一样 - 将是

和后者用例的revrange一样有用,AND非常方便我在很多情况下使用xrange并且每次使用每个人并且每次都要吝啬
。另外,具体来说,我有一些用例:


如果必须反转(...):

seq = xrange(N-1,-1, -1)

否则:

seq = xrange(N)

for seq中的项目:

.. 。


和那些可以从反向或

revrange获得一些小的好处,因为if / else准备seq仍然会在那里,

虽然在两个分支中的一个分支上更具可读性。但是,对于这种

类型的用例,带有可选reverse =的irange将是完美的:


for item中的项目(N,reverse = mustbereversed (...)):

...


现在确实很幸福。


我opine irange-w-opt-parm是迄今为止最好的解决方案。例如,你的用例来自heapq.heapify的
,关于定制为

revrange,在我看来有三种可能性:

for i反转(xrange(n // 2)):

...

for i in revrange(n // 2):

...
我在irange中的
(n // 2,反向=真):

...

几乎相当于 - - 即使在这里,revrange的简洁也是一个非常好的
小额福利。事实上,范围广泛的用户可能会让我们慢慢地逐渐开始劝阻。

使用范围和xrange - 这将是一天......即使自迭代器协议出现以来,我已经为了一个irange而感到痛苦。而使用

的reverse = True无论如何,成语将是普遍的和习惯性的

,因为它现在也存在于list.sort中。


(_and_,恕我直言, irange作为内置函数具有100%的适当性,因为它会/应该很快成为***中使用最广泛的

构造之一*** - FAR不只是翻新或反转......!)。

Alex



It would deal more directly with about half of my use cases but
less directly with the other half, so that''s roughly a wash. But
having revrange return a list would just perpetuate the minor wart
that range has now become, and having it NOT return a list (rather
an iterator) would violate the principle of Least Surprise wrt range.

A built-in iterator irange -- with an optional "reverse=" argument,
just like the very useful one you recently added to list.sort -- would be
just as useful as revrange for the latter''s use cases, AND be very handy to
me in many more cases in which I currently use xrange and cringe each and
every time. Plus, specifically, I have some uses cases where:

if mustbereversed(...):
seq = xrange(N-1, -1, -1)
else:
seq = xrange(N)
for item in seq:
...

and those would get a somewhat minor benefit from either reverse OR
revrange, since the if/else to prepare seq would still be there,
albeit more readable along one of the two branches. However, for this
kind of use case, an irange with an optional reverse= would be PERFECT:

for item in irange(N, reverse=mustbereversed(...)):
...

now THAT would be blissful indeed.

I opine irange-w-opt-parm would be by far the best solution. E.g.,
in your use case from heapq.heapify, just about custommade for
revrange, in my opinion the three possibilities:
for i in reversed(xrange(n//2)):
...
for i in revrange(n//2):
...
for i in irange(n//2, reverse=True):
...
are just about equivalent -- the concision of revrange is a very
minor benefit even here. The vastly wider usecases of irange might
in fact even let us slowly and gradually start "discouraging" the
use of range and xrange -- that will be the day... I''ve pined for
an irange even since the iterator protocol appeared. And the use
of the "reverse=True" idiom will be widespread and habitual anyway
thanks to the fact that it''s present in list.sort as well now.

(_and_, IMHO, irange has 100% appropriateness as a builtin, given
that it would/should soon become ***one of the most widely used
constructs in Python*** -- FAR more than revrange or reversed...!).
Alex


" Raymond Hettinger" < VZ ****** @ verizon.net>在消息新闻中写道:< cy ******************* @ nwrdny01.gnilink.net>。 ..
"Raymond Hettinger" <vz******@verizon.net> wrote in message news:<cy*******************@nwrdny01.gnilink.net>. ..
根据comp.lang.python上的反馈,pep已经更新:

www.python.org/peps/pep-0322.html
Based on the feedback here on comp.lang.python, the pep has been
updated:

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




考虑到这个想法,我意识到我的用例反转

会非常小:绝对不足以证明新的

内置。 OTOH,我不会反对授权枚举

我一直在使用。类似


枚举(可迭代,''反转'')


或变体。


我个意见,


Michele



Thinking about the idea, I realized that my use case for "reversed"
would be very very little: definitely, not enough to justify a new
built-in. OTOH, I would not be opposed to empowering "enumerate"
which I use all the time. Something like

enumerate(iterable,''reversed'')

or variations.

Just my opinion,

Michele


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

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