What for -- for? (was A bug?)

Rustom Mody rustompmody at gmail.com
Thu Oct 30 18:41:37 CET 2014


On Thursday, October 30, 2014 10:53:13 PM UTC+5:30, Joshua Landau wrote:
> On 29 October 2014 03:22, Rustom Mody  wrote:
> > Yesterday I was trying to introduce python to some senior computer scientists.
> >
> > Tried showing a comprehension-based dir-walker vs a for-loop based one:
> >
> > def dw(p):
> >    if isfile(p):
> >       return [p]
> >    else:
> >       return [p] + [c for f in listdir(p) for c in dw(p+'/'+f)]
> >
> ...
> >
> > Comment to me : "Well this is neat and compact, but it does not add
> > anything fundamental (over usual index based for-loops)"
> >
> > I tried to say that 'for' over general sequences is quite different
> > and significantly more powerful than C/Pascal for over indexes +
> > explicit indexing.
> 
> If you really want to show the generality of iteration, I suggest you
> start with iterators:
> 
>     def walk(path):
>         yield path
> 
>         if isdir(path):
>             for name in iterdir(path):
>                 for file in walk(path + "/" + name):
>                     yield file

heh!
That was my next version -- almost word-for-word
[Not on that laptop now; will check later]



More information about the Python-list mailing list