loops -> list/generator comprehensions

Caleb Hattingh caleb1 at telkomsa.net
Mon Feb 7 22:53:05 EST 2005


Wow, Steve, thanks, you went to some effort here.

> I prefer to give names to the values produced by os.walk -- I think it  
> makes the usage much clearer.  However, since I don't use 'dirnames', I  
> use '_' to indicate this:

Actually, I feel silly for not recognising this - I read about the Python3  
suggestion for adding a "with" syntax, and the suggestion was rather to  
use something like

   _ = instname
   _.a = 1
   _.b = 2

So I actually have seen this "_" placeholder before :)  Sorry bout that.

> Would
>
>      filenames = [os.path.join(dirpath, filename)
>                   for dirpath, dirnames, filenames in os.walk('.')
>                   for filename in filenames]
>
> have been clearer for you?  Then all you have to do is remember the  
> order of the for-loop execution:

Bizarre as this may sound, it was the '_' that was throwing me off the  
whole thing (at the 'grok' level I generally read the newsgroup,  
anyway).   For some weird reason, I can read *this* comprehension pretty  
easily!  Does that make sense at all?  I figure a little bit of  
uncertainty along the way probably derails understanding of the whole  
thing a little bit - and (mental note) I *must* remember this when I  
explain stuff to people at work, having now experienced it first hand.

Thanks again
Caleb




More information about the Python-list mailing list