Alex Martelli wrote, replying to ?!ing:
Forms such as:
[ f(23) for f in stuff if callable(f) ]
have been in Python for years now, and you could indeed say "the very first thing you see" (the call f(23)) is "getting short-circuited" by an if-guard at the very end.
I think one reason why that isn't so troublesome is that you can think of it as doing the filtering first: it's the same as [f(23) for f in [f for f in stuff if callable(f)]] where the only "short-circuiting" is the trivial sort equivalent to a call to "filter". Except that the unexpanded form is shorter and clearer and avoids a gratuitous list allocation, of course. But, actually, I've always felt slightly queasy about the syntax for filtering in listcomps, because of the way that it interrupts the "right-to-left" dataflow. I'm not sure [f(23) if callable(f) for f in stuff] is really any better, though. Maybe it should have been [for f in stuff: if callable(f): f(23)] <0.5 tim>. Actually, I *like* that, because it makes the behaviour of nested listcomps clearer. Oh well. * Speaking of filtering listcomps, am I unusual in thinking it would be nice if you could say "[t in I if C]" instead of "[t for t in I if C]" when all you want is the filtering effect? I suspect I may be, since the shorter form is less logical than the longer. Perhaps the main reason why I want it is that it seems to be what my fingers always type for me when I write a filtering listcomp. -- g
participants (1)
-
Gareth McCaughan