1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
26
Rep Power
0

#### Generator Problem

Giving the scenario below. I want to be able to say, if generator has no Items do something else

Usage:
2. Something like this ...
Code:
```def abc_gen():
yield "a"
yield "b"
yield "c"

ga = abc_gen()
while True:
try:
print(next(ga))
except StopIteration:
break

print('-'*12)

# generator expression
gn = (x for x in range(4))

while True:
try:
print(next(gn))
except StopIteration:
break

''' result ...
a
b
c
------------
0
1
2
3
'''

# refresh the generator
ga = abc_gen()
# notice that an iterator like a for loop
# raises StopIteration automatically
for c in ga:
print(c)

''' result ...
a
b
c
'''```
Last edited by Dietrich; February 12th, 2013 at 03:29 PM.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
26
Rep Power
0
Originally Posted by Dietrich
Something like this ...
Code:
```def abc_gen():
yield "a"
yield "b"
yield "c"

ga = abc_gen()
while True:
try:
print(next(ga))
except StopIteration:
break

print('-'*12)

# generator expression
gn = (x for x in range(4))

while True:
try:
print(next(gn))
except StopIteration:
break

''' result ...
a
b
c
------------
0
1
2
3
'''

# refresh the generator
ga = abc_gen()
# notice that an iterator like a for loop
# raises StopIteration automatically
for c in ga:
print(c)

''' result ...
a
b
c
'''```
Thanks