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".

Cheers,
-- 
Cameron Simpson <c...@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

The Borg assimilated my race and all I got was this lousy tagline.
        - Cath Lawrence <cath_lawre...@premium.com.au>
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to