Generator Question

Discussion in 'Python' started by GZ, Dec 22, 2011.

  1. GZ

    GZ Guest

    Hi,

    I am wondering what would be the best way to return an iterator that
    has zero items.

    I just noticed the following two are different:

    def f():
    pass
    def g():
    if 0: yield 0
    pass

    for x in f(): print x
    Traceback (most recent call last):
    File "<string>", line 1, in <fragment>
    TypeError: 'NoneType' object is not iterable

    for x in g(): print x
    #loop exits without any errors

    Now the question here is this:

    def h():
    if condition=true:
    #I would like to return an itereator with zero length
    else:
    for ...: yield x

    In other words, when certain condition is met, I want to yield
    nothing. How to do?

    Thanks,
    gz
     
    GZ, Dec 22, 2011
    #1
    1. Advertising

  2. On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote:

    > Hi,
    >
    > I am wondering what would be the best way to return an iterator that has
    > zero items.


    return iter([])


    > I just noticed the following two are different:
    >
    > def f():
    > pass


    That creates a function that does nothing, and then returns None (because
    Python doesn't have Pascal procedures or C void function).


    > def g():
    > if 0: yield 0
    > pass


    The pass is redundant.

    This creates a generator function which, when called, doesn't yield
    anything, then raises StopIteration.



    --
    Steven
     
    Steven D'Aprano, Dec 22, 2011
    #2
    1. Advertising

  3. On Thu, Dec 22, 2011 at 4:45 PM, GZ <> wrote:
    > def h():
    >    if condition=true:
    >       #I would like to return an itereator with zero length
    >    else:
    >       for ...: yield x


    Easy. Just 'return' in the conditional.

    >>> def h():

    if condition:
    return
    for i in range(4): yield i

    >>> condition=False
    >>> h()

    <generator object h at 0x01913E68>
    >>> for i in h(): print(i)


    0
    1
    2
    3
    >>> condition=True
    >>> h()

    <generator object h at 0x01079E40>
    >>> for i in h(): print(i)


    >>>


    A generator object is returned since the function contains a 'yield'.
    On one of the branches, nothing will ever be yielded and StopIteration
    will be raised immediately.

    You could probably also raise StopIteration directly, but it's not necessary.

    ChrisA
     
    Chris Angelico, Dec 22, 2011
    #3
  4. On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote:

    > Now the question here is this:
    >
    > def h():
    > if condition=true:
    > #I would like to return an itereator with zero length
    > else:
    > for ...: yield x
    >
    > In other words, when certain condition is met, I want to yield nothing.
    > How to do?


    Actually, there's an even easier way.

    >>> def h():

    .... if not condition:
    .... for c in "abc":
    .... yield c
    ....
    >>>
    >>> condition = False
    >>> list(h())

    ['a', 'b', 'c']
    >>> condition = True
    >>> list(h())

    []




    --
    Steven
     
    Steven D'Aprano, Dec 22, 2011
    #4
  5. GZ

    GZ Guest

    I see. Thanks for the clarification.

    On Dec 22, 12:35 am, Steven D'Aprano <steve
    > wrote:
    > On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote:
    > > Now the question here is this:

    >
    > > def h():
    > >     if condition=true:
    > >        #I would like to return an itereator with zero length
    > >     else:
    > >        for ...:yieldx

    >
    > > In other words, when certain condition is met, I want toyieldnothing.
    > > How to do?

    >
    > Actually, there's an even easier way.
    >
    > >>> def h():

    >
    > ...     if not condition:
    > ...         for c in "abc":
    > ...            yieldc
    > ...
    >
    > >>> condition = False
    > >>> list(h())

    > ['a', 'b', 'c']
    > >>> condition = True
    > >>> list(h())

    >
    > []
    >
    > --
    > Steven
    >
    >
     
    GZ, Dec 24, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maurer
    Replies:
    3
    Views:
    5,098
    Peter
    Apr 19, 2006
  2. TheDustbustr
    Replies:
    1
    Views:
    498
    Sami Hangaslammi
    Jul 25, 2003
  3. Replies:
    9
    Views:
    615
  4. Chris Withers

    Problems with email.Generator.Generator

    Chris Withers, Sep 11, 2006, in forum: Python
    Replies:
    20
    Views:
    1,763
    Max M
    Sep 12, 2006
  5. Terry Reedy

    Generator functions subclass generator?

    Terry Reedy, Jun 18, 2009, in forum: Python
    Replies:
    0
    Views:
    503
    Terry Reedy
    Jun 18, 2009
Loading...

Share This Page