Python中需要yield_all [英] yield_all needed in Python

查看:76
本文介绍了Python中需要yield_all的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写生成器时,我只是在想Python如何需要一个

" yield_all"声明。在谷歌的帮助下,我在Lightweight

语言邮件列表中发现了一个

之前的讨论。我会在这里重新发布它,以便提高某天实际发生的这种增强的机会。来自LL邮件列表的原始

海报似乎主要关注

算法效率,而我更关心的是制作我的

程序更短更容易阅读。随后关于LL

列表的讨论讨论了如果想要获得所需的效率增益,yield_all将如何实施有点难以实现,但我不会

如果那个目标不是b
$ b b b b b b that that that that that that that that that that that that that that that that that

Python的问题''收益''


*收件人:LL1邮件列表<地址@隐藏>

*主题:Python的问题''收益''

*来自:Eric Kidd< address @ hidden>

*日期:2003年5月27日11:15 :20 -0400

*组织:

*发件人:地址@隐藏


我要选择Python在这里,但仅仅因为示例代码将是简短而甜蜜的。 :-)我相信

生成器的其他几个实现也有同样的问题。

Python的生成器系统,天真地使用,转O(N )树遍历

进入O(N log N)树遍历:


类树:

def __init __(自我, value,left = None,right = None):

self.value = value

self.left = left

self.right = right


def in_order(self):

如果self.left不是None:

for v in self.left.in_order ():

收益率v

收益率自我价值

如果self.right不是None:

for v in self.right.in_order():

收益率v

t =树(2,树(1),树(3))

for v in yield_bug.t.in_order():

print v


打印:

1

2

3


不幸的是,这个片段称收益率为5倍,因为叶子

值必须在wa上产生两次y备份树。


我们可以通过添加新的

关键字缩短代码 - 并使其在O(N)时间内运行替换for v in ...:yield v模式:


def in_order(self):

如果self.left不是None:

yield_all self.left.in_order ():

收益self.value

如果self.right不是None:

yield_all self.right.in_order():


有趣的是,这允许你定义诸如

" tail-recursive generation"之类的概念,然后应用通常的包包

递归优化技术。


干杯,

Eric


|> oug

解决方案

2005年2月28日星期一18:25:51 -0500,Douglas Alan写道:

在写一个发电机时,我只是在想Python如何需要
yield_all声明。在谷歌的帮助下,我在Lightweight Languages
邮件列表中发现了一个关于此问题的预先讨论。我会在这里重新发布它,以便提高某一天实际发生这种增强的可能性。




您还应该查找对此的响应。蒂姆彼得的

响应可从
http://aspn.activestate.com/ASPN/Mail/Message/624273

链接自
http://aspn.activestate.com/ASPN/Mai...hon-dev/758572

这是最相关的部分。


我没有打扰 - 这是领土。如果/当

成熟的协同程序也是如此,人们担心这可能会使用它们而不是
。奇怪的事实:我*担心简单生成器的最坏情况时间方面。在Icon几年前,但在实践中

从来没有被它烧过。并重写使用Icon

共同表达式的东西总是会导致更糟糕的代码在几乎所有情况下都显得慢得多,除了那些我

*设计*来证明O()差异。


顺便说一句,Python几乎从不担心最坏情况的行为,而且人们使用Python来b / b
而不是,例如,平衡的树木,每天早些时候与他们携带他们的耻辱回家< wink> 。


Andrew
da***@dalkescientific.com




" Douglas Alan" <是ne **** @ mit.edu>在消息中写道

news:lc ************ @ gaffa.mit.edu ...

我们可以缩短代码 - 并使其在O(N)时间运行 - 通过添加一个新的
关键字来替换for v in ...:yield v模式:


也许。在定义yield_all的语义并至少概述

实现之前,我不相信''在o(n)时间运行''。曾经有一个

的几个帖子讨论过以某种方式产生收益的相关想法,

神奇地,跳过只产生价值的中间发电机,

没有变形。但是,在没有对所有发电机产生负面影响的情况下,几乎不知道如何实现这一点。 Cetainly,如果< yield_all

iterator> ==< for it in iterator:yield i> ;,我看不出有什么东西是

除了几次击键外。如果< yield_all iterator> ==< yield

list(i for it in iterator)>然后替换是一个语义变化。

def in_order(self):
如果self.left不是None:
yield_all self.left.in_order():
yield self.value
如果self.right不是None:
yield_all self.right.in_order():




如果我当写一个基于文本的双递归到迭代变换器,

a pseudokeyword可能是一个想法,表明堆叠的产量是

识别函数,因此可以绕过。


Terry J. Reedy


Terry Reedy写道:

" Douglas Alan" <是ne **** @ mit.edu>在消息中写道
新闻:lc ************ @ gaffa.mit.edu ...

我们可以缩短代码 - 并使其在O(N)时间运行 - 通过添加一个新的
关键字来替换for v in ...:yield v模式:

也许吧。在定义yield_all的语义并至少概述
实现之前,我不相信''在o(n)时间运行''。曾经有一个关于某种相关想法的几个帖子的讨论,不知何故产生了收益,奇迹般地,跳过只产生价值的中间发电机,没有转换。但是,在没有对所有发电机产生负面影响的情况下,几乎不知道如何做到这一点。 Cetainly,如果< yield_all
iterator> ==< for it in iterator:yield i> ;,除了几次击键之外,我什么都看不到。如果< yield_all iterator> ==< yield
list(i for it in iterator)>然后更换是一个语义变化。



La plus ca change,la plus c''est la meme选择(我相信原生法语

发言者会借口导致没有重音的懒惰。


这让人想起几年前关于尾巴的讨论

递归以及它如何变得很棒优化边缘情况的事情。

当然我们当时没有发电机,所以我们不能抱怨它们*效率低的原因。

def in_order(self):
如果self.left不是None:
yield_all self.left.in_order():
产生self.value
如果self.right不是None:
yield_all self.right.in_order():



当我向迭代变换器写一个基于文本的双递归时,识别fu因此可以绕过。



上面的关键词是使用。我怀疑和案件。


python:-always-something-new-to-bitch-about-ly y''rs - steve


While writing a generator, I was just thinking how Python needs a
"yield_all" statement. With the help of Google, I found a
pre-existing discussion on this from a while back in the Lightweight
Languages mailing list. I''ll repost it here in order to improve the
chances of this enhancement actually happening someday. The original
poster from the LL mailing list seems mostly concerned with
algorithmic efficiency, while I''m concerned more about making my
programs shorter and easier to read. The ensuing discussion on the LL
list talks about how yield_all would be somewhat difficult to
implement if you want to get the efficiency gain desired, but I don''t
think it would be very difficult to implement if that goal weren''t
required, and the goal were limited to just the expressive elegance:

A Problem with Python''s ''yield''

* To: LL1 Mailing List <address@hidden>
* Subject: A Problem with Python''s ''yield''
* From: Eric Kidd <address@hidden>
* Date: 27 May 2003 11:15:20 -0400
* Organization:
* Sender: address@hidden

I''m going to pick on Python here, but only because the example code will
be short and sweet. :-) I believe several other implementations of
generators have the same problem.

Python''s generator system, used naively, turns an O(N) tree traversal
into an O(N log N) tree traversal:

class Tree:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right

def in_order(self):
if self.left is not None:
for v in self.left.in_order():
yield v
yield self.value
if self.right is not None:
for v in self.right.in_order():
yield v

t=Tree(2, Tree(1), Tree(3))
for v in yield_bug.t.in_order():
print v

This prints:
1
2
3

Unfortunately, this snippet calls ''yield'' 5 times, because the leaf
values must be yielded twice on their way back up the tree.

We can shorten the code--and make it run in O(N) time--by adding a new
keyword to replace the "for v in ...: yield v" pattern:

def in_order(self):
if self.left is not None:
yield_all self.left.in_order():
yield self.value
if self.right is not None:
yield_all self.right.in_order():

Interestingly enough, this allows you define notions such as
"tail-recursive generation", and apply the usual bag of
recursion-optimization techniques.

Cheers,
Eric

|>oug

解决方案

On Mon, 28 Feb 2005 18:25:51 -0500, Douglas Alan wrote:

While writing a generator, I was just thinking how Python needs a
"yield_all" statement. With the help of Google, I found a pre-existing
discussion on this from a while back in the Lightweight Languages
mailing list. I''ll repost it here in order to improve the chances of
this enhancement actually happening someday.



You should also have looked for the responses to that. Tim Peter''s
response is available from
http://aspn.activestate.com/ASPN/Mail/Message/624273
as linked from
http://aspn.activestate.com/ASPN/Mai...hon-dev/758572
Here is the most relevant parts.

I''m not bothered -- this comes with the territory. If/when
full-fledged coroutines make it in too, people worried about that can
use them instead. Curious fact: I *was* worried about the worst-case
time aspects of "simple generators" in Icon years ago, but in practice
never ever got burned by it. And rewriting stuff to use Icon
co-expressions instead invariably resulted in messier code that ran
significantly slower in virtually all cases, except for the ones I
*contrived* to prove the O() difference.

BTW, Python almost never worries about worst-case behavior, and people
using Python dicts instead of, e.g., balanced trees, get to carry their
shame home with them hours earlier each day <wink> .

Andrew
da***@dalkescientific.com



"Douglas Alan" <ne****@mit.edu> wrote in message
news:lc************@gaffa.mit.edu...

We can shorten the code--and make it run in O(N) time--by adding a
new
keyword to replace the "for v in ...: yield v" pattern:
Maybe. Until you define the semantics of yield_all and at least outline an
implementation, I am not convinced of ''run in o(n) time''. There was once a
several-post discussion of a related idea of having yield somehow,
magically, skip intermediate generators that only yielded value on up,
without tranformation. But it was never clear how to do this practically
without negatively impacting all generators. Cetainly, if <yield_all
iterator> == <for i in iterator: yield i>, I don''t see how anything is
gained except for a few keystrokes. If <yield_all iterator> == <yield
list(i for i in iterator)> then the replacement is a semantic change.
def in_order(self):
if self.left is not None:
yield_all self.left.in_order():
yield self.value
if self.right is not None:
yield_all self.right.in_order():



If and when I write a text-based double-recursion to iteration transformer,
a pseudokeyword might be be an idea for indicating that stacked yields are
identify functions and therefore bypassable.

Terry J. Reedy


Terry Reedy wrote:

"Douglas Alan" <ne****@mit.edu> wrote in message
news:lc************@gaffa.mit.edu...

We can shorten the code--and make it run in O(N) time--by adding a
new
keyword to replace the "for v in ...: yield v" pattern:

Maybe. Until you define the semantics of yield_all and at least outline an
implementation, I am not convinced of ''run in o(n) time''. There was once a
several-post discussion of a related idea of having yield somehow,
magically, skip intermediate generators that only yielded value on up,
without tranformation. But it was never clear how to do this practically
without negatively impacting all generators. Cetainly, if <yield_all
iterator> == <for i in iterator: yield i>, I don''t see how anything is
gained except for a few keystrokes. If <yield_all iterator> == <yield
list(i for i in iterator)> then the replacement is a semantic change.


La plus ca change, la plus c''est la meme chose (I trust native French
speakers will excuse the laziness that led to the absence of accent).

This is very reminiscent of discussions several years ago about tail
recursion and how it would be a great thing to optimise the edge cases.
Of course we didn''t have generators then, so we couldn''t complain about
*their* inefficiencies then.

def in_order(self):
if self.left is not None:
yield_all self.left.in_order():
yield self.value
if self.right is not None:
yield_all self.right.in_order():


If and when I write a text-based double-recursion to iteration transformer,
a pseudokeyword might be be an idea for indicating that stacked yields are
identify functions and therefore bypassable.


The key words in the above being "use" and "case", I suspect.

python:-always-something-new-to-bitch-about-ly y''rs - steve


这篇关于Python中需要yield_all的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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