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 -- http://mail.python.org/mailman/listinfo/python-list