Hi Cameron, On 2010-10-25 01:08, Cameron Simpson wrote: > On 24Oct2010 20:58, Stefan Schwarzer <sschwar...@sschwarzer.net> wrote: > | On 2010-10-21 00:27, Sebastian wrote: > | > Is there a simpler way to yield all elements of a sequence than this? > | > for x in xs: > | > yield x > | > | Can you give an example where you would need this? Can't > | you just iterate over the sequence? > > The usual example is when the sequence comes from inside. > Example, closely resembling some code from on of my projects: > > def leaves(N): > if N.isleaf: > yield N > for subN in N.subnodes: > for leaf in leaves(subN): > yield leaf > > which walks a tree structure returning leaf nodes. > > The point is that you're calling leaves() on the subnode and yiled them > directly to the outside. The caller may not even know there are "subnodes".
>From the question and the code snippet the OP gave I assumed he meant that there already was a sequence (i. e. linear structure) to begin with. By the way, I think a well-known example of what you describe is `os.walk`. Stefan -- http://mail.python.org/mailman/listinfo/python-list