是的,这是一个很好的观点,这也是我倾向于现在更喜欢发电机的原因之一。但无论如何,这只是我,或者'链式的'/ b $ b'收益率'看起来对别人来说难看吗?这可能只是一个问题,即用它来赚取b $ b,但不过它却让我烦恼...... - Carlos Ribeiro Consultoria em Projetos 博客: http: //rascunhosrotos.blogspot.com 博客: http:/ /pythonnotes.blogspot.com 邮件: ca ******* *@gmail.com 邮件: ca ******** @ yahoo.com Carlos Ribeiro< ca ******** @ gmail.com>写道: ... 是的,这是一个很好的观点,这也是我倾向于选择现在的发电机的原因之一。但无论如何,它只是我,或者链式的产量看起来对别人来说难看吗?这可能只是一个习惯了它的问题,但它仍然困扰着我...... 我不知道为什么它应该看起来很难看。什么替代方案 你认为普通(在任何发电机中)非丑陋的成语: for x in y:yield x ? 也许有些语法破解,例如新的语句关键字''yieldall'' 其中上面的成语可以表示为 yieldall y ?或者其他一些语法黑客,比如''yield<< y'',或''从y'收益', 同上?就个人而言,在我看来,这样的特殊语法只需要学习一点Python,没有实质性的好处, 但这是因为我看到了在今天的成语中没有任何丑陋的东西 - 它只是说明了它的内容,简单而朴素...... Alex Hello all,Here I am using some deeply nested, tree-like data structures. In somesituations I need to traverse the tree; the old-style way to do it isto write a recursive method on the node class, as in:def walk(self):"""old-style recursive tree traversal"""child.do_somethingfor child in childs:child.walk()Of course, "do_something" can be a configurable method or action onthe node, whose actual implementation doesn''t matter for thisdiscussion. The recursive solution is clean and readable, and that''swhy its frequently preferred over the iterative solution, which (a)requires manual housekeeping (using a stack) to work; and (b) is nottruly ''object oriented'', in the sense that it does not delegate nodetraversal behavior to the node being traversed.In my last projects I''ve been tending towards a greater use ofgenerators. However, when a generator is used in the situationdescribed above, the end result does not ''read'' as naturally:def walk(self):"""generator-based recursive tree traversal"""yield childfor child in childs:for node in child.walk()yield nodeThe problem is that I need to ''chain'' the yields from the subtrees toyield back the result to the caller. It does not seem to be as elegantas the simple recursive version; it''s confusing to read, because onehas to figure out what is being yielded in the inner loop, and it''salso error-prone -- it''s easy to forget to include the ''yield'' in theinner loop.I''m now wondering which is better: to keep using old-style recursivetree traversal code, or to chained-yield generator solution. It''s moreof a stylistic decision -- I''m not concerned about raw performance atthis point, but clarity and elegancy of design are more important.--Carlos RibeiroConsultoria em Projetosblog: http://rascunhosrotos.blogspot.comblog: http://pythonnotes.blogspot.commail: ca********@gmail.commail: ca********@yahoo.com 解决方案 Carlos Ribeiro wrote: Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node class, as in: def walk(self): """old-style recursive tree traversal""" child.do_something for child in childs: child.walk()[...] def walk(self): """generator-based recursive tree traversal""" yield child for child in childs: for node in child.walk() yield node[...] I''m now wondering which is better: to keep using old-style recursive tree traversal code, or to chained-yield generator solution. It''s more of a stylistic decision -- I''m not concerned about raw performance at this point, but clarity and elegancy of design are more important.You must also consider the code that uses the iteration, at least if youwant to walk the structure for multiple purposes# In the recursive casedef nodeHandler (node):node.do_something ()root.walk (nodeHandler)# in the generator casefor node in root.walk ():node.do_something ()And the latter can also be fed into routines that expect an iterator.Compare also os.path.walk with os.walk.DanielOn Mon, 18 Oct 2004 14:11:53 +0200, Daniel Dittmar<da************@sap.corp> wrote: You must also consider the code that uses the iteration, at least if you want to walk the structure for multiple purposes <snip> Compare also os.path.walk with os.walk. Daniel -- http://mail.python.org/mailman/listinfo/python-listYes, that''s a good point, and it''s one of the reasons I tend to preferthe generator now. But anyway, it''s just me, or does the ''chainedyield'' look ugly to someone else? It may be just a question of gettingused to it, but nevertheless it''s bugging me...--Carlos RibeiroConsultoria em Projetosblog: http://rascunhosrotos.blogspot.comblog: http://pythonnotes.blogspot.commail: ca********@gmail.commail: ca********@yahoo.comCarlos Ribeiro <ca********@gmail.com> wrote:... Yes, that''s a good point, and it''s one of the reasons I tend to prefer the generator now. But anyway, it''s just me, or does the ''chained yield'' look ugly to someone else? It may be just a question of getting used to it, but nevertheless it''s bugging me...I''m not sure why it should look ugly to you. What alternatives wouldyou consider non-ugly for the common (in any generator) idiom:for x in y: yield x?Perhaps some syntax hack such as a new statement keyword ''yieldall''where the above idiom could be expressed asyieldall y? Or some other syntax hack such as ''yield<<y'', or ''yield from y'',ditto? Personally, it would seem to me that such special syntax wouldbe just one more bit of Python to learn, without substantial benefits,but that''s because I see nothing ugly in today''s idiom -- it does justwhat it says, plainly and unpretentiously...Alex 这篇关于关于递归发生器的样式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 22:43