What for -- for? (was A bug?)
Joshua Landau
joshua at landau.ws
Thu Oct 30 13:21:56 EDT 2014
More information about the Python-list mailing list
Thu Oct 30 13:21:56 EDT 2014
- Previous message (by thread): Finding way around ABCs (was What for -- for? (was A bug?))
- Next message (by thread): What for -- for? (was A bug?)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 29 October 2014 03:22, Rustom Mody <rustompmody at 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)").
- Previous message (by thread): Finding way around ABCs (was What for -- for? (was A bug?))
- Next message (by thread): What for -- for? (was A bug?)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list