[Tutor] Generator expressions...

Modulok modulok at gmail.com
Sun Feb 27 23:23:28 CET 2011


>> 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?

I knew it was subtle. I was trying to use 'read(4096)' like open()'s
buffersize parameter. (Which it's obviously isn't.)

Thanks!
-Modulok-


More information about the Tutor mailing list