__iter__ yield

Diez B. Roggisch deets at nospam.web.de
Sun Mar 9 23:00:08 CET 2008


duccio schrieb:
> 
> Hello!
> Someone knows if it's possible to make this __iter__ function with just 
> one 'yield' intead of two?
> Is there some simpler way to make this  __iter__ iter through all nodes?
> Thanks!
> 
> class Node:
>     def __init__(self, data=None):
>         self.childs=[]
>         self.data=data
>     def appendNode(self, n):
>         node=Node(n)
>         self.childs.append(node)
>         return node
>     def __str__(self):
>         return '<'+str(self.data)+'>'
>     def __iter__(self):
>         yield self #1
>         for n in self.childs:
>             for nn in n.__iter__():
>                 yield nn #2

Nope. There have been numerous discussions about this, introducing 
something like a yield_all-keyword or such thing that would replace the 
above boilerplate - but so far they all have been rejected. Search the 
archives for the reasons, I don't remember them :)

Diez



More information about the Python-list mailing list