Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Generator Question

Reply
Thread Tools

Generator Question

 
 
GZ
Guest
Posts: n/a
 
      12-22-2011
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





 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-22-2011
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
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      12-22-2011
On Thu, Dec 22, 2011 at 4:45 PM, GZ <(E-Mail Removed)> 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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-22-2011
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
 
Reply With Quote
 
GZ
Guest
Posts: n/a
 
      12-24-2011
I see. Thanks for the clarification.

On Dec 22, 12:35*am, Steven D'Aprano <steve
(E-Mail Removed)> 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
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generator functions subclass generator? Terry Reedy Python 0 06-18-2009 09:51 PM
Problems with email.Generator.Generator Chris Withers Python 20 09-12-2006 11:13 AM
UART with fractional baudrate generator ? Or fractional baudrate generator alone Martin Maurer VHDL 3 04-19-2006 01:26 PM
subtle side effect of generator/generator expression bonono@gmail.com Python 9 10-16-2005 06:42 PM
generator function within a generator function doesn't execute? TheDustbustr Python 1 07-25-2003 10:45 AM



Advertisments