On Sun, Feb 27, 2011 at 10:34 PM, Modulok <modu...@gmail.com> wrote:
>
> import hashlib
>
> fd = open('/dev/urandom', 'rb')
> gen = (hashlib.sha256(i).hexdigest() for i in fd.read(4096))
>
> try:
>    for i in gen:
>        print i     #<-- This loop should never end... but does. Why?
>
> except KeyboardInterrupt:
>    gen.close()
>    fd.close()
>    print "\nBye!"
>

Check out the generator expression. What are you iterating over? How
long is the string returned by the read? Obviously, it isn't of
infinite length. Then why are you expecting the generator expression
to run forever? Remember that a generator expression isn't like a
function. You can't call it multiple times. You iterate over it once,
then it's done, and you'll have to create a new one.

HTH,
Hugo
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to