On 29 October 2014 03:22, Rustom Mody <rustompm...@gmail.com> 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 This is fundementally inexpressable with indexes. It also lends itself to expressing delegation (eg. "yield from walk(path + "/" + name)"). -- https://mail.python.org/mailman/listinfo/python-list