En Mon, 28 May 2007 05:25:18 -0300, Maric Michaud <[EMAIL PROTECTED]>  
escribió:

> Gabriel Genellina a écrit :
>> - iterate backwards:
>>
>> for i in range(len(names)-1, -1, -1):
>>    fname = names[i]
>>    if fname[:1]=='.':
>>      names.remove(fname)
>>
>
> This is not about iterating backward, this is about iterating over the
> index of each element instead of iterating over the element (which must
> be done begining by the end). In fact this code is both inefficient and
> contains a subtle bug. If two objects compare equals in the list, you
> will remove the wrong one.
>
> It should be :
>
> for i in range(len(names)-1, -1, -1):
>     if names[i][:1]=='.':
>       del names[i]

Yes, sure, this is what I should have written. Thanks for the correction!

>> - filter and reassign in place
>
> Seems the best here.
>
>> (the [:] is important):
>
> Not so. Unless "names" is referenced in another namespace, simple
> assignment is enough.

But this is exactly the case; the visit function is called from inside the  
os.path.walk code, and you have to modify the names parameter in-place for  
the caller to notice it (and skip the undesided files and folders).

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to