Generators can only yield ints?

Wojtek Walczak gminick at bzt.bzt
Sat Aug 23 01:08:35 CEST 2008


On Fri, 22 Aug 2008 15:44:15 -0700 (PDT), defn noob wrote:
> def letters():
> 	a = xrange(ord('a'), ord('z')+1)
> 	B = xrange(ord('A'), ord('Z')+1)
> 	while True:
> 		yield chr(a)
> 		yield chr(B)
>
>
>>>> l = letters()
>>>> l.next()
>
> Traceback (most recent call last):
>   File "<pyshell#225>", line 1, in <module>
>     l.next()
>   File "<pyshell#223>", line 5, in letters
>     yield chr(a)
> TypeError: an integer is required
>>>>

The error you're seeing is a result of passing
non-integer to chr() function, it has nothing to do
with generators:

>>> chr([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>>

I have simplified your code a bit:
-------------
import string
def letters():
        a, B = (string.letters,) * 2
        for i in zip(a, B):
                yield i[0]
                yield i[1]

l = letters()

print l.next()
print l.next()
print l.next()
--------------

and the output:

$ python genlet.py
a
a
b
$

Is that what you tried to achieve?

-- 
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/



More information about the Python-list mailing list