the usage of 'yield' keyword

Dave Angel davea at ieee.org
Thu Oct 15 23:26:59 EDT 2009


Tim Golden wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Dave 
> Angel wrote:
>
>> def find(root):
>>    for pdf in os.walk(root, topdown=False):
>>            for file in pdf[2]:
>>                yield os.path.join(pdf[0],file)
>>
>>
>
>
> At the risk of nitpicking, I think that a modicum of
> tuple-unpacking would aid readability here:
>
> <code>
>
> for dirpath, dirnames, filenames in os.walk (root, topdown=False):
>  for filename in filenames:
>    yield os.path.join (dirpath, filename)
>
> </code>
>
> TJG
>
> </div>
>
Absolutely, readability is paramount.  But in my defense, I was trying 
to make the minimal change necessary to the code that had been already 
posted, just to turn it into a generator.  I didn't even notice why the 
pdf name was used till you pointed out the expansion.

DaveA



More information about the Python-list mailing list