by Smerity · 14y ago · 0 comments
As far as list comprehensions, I tend to find list comprehensions the readable and self documenting, at least in Python where I most commonly see them. If nothing else, what is the ordering of the arguments for the equivalent non list comprehension? With list comprehensions you don't have that impedance.
// Python example with filter vs list comprehension
// Filter requires you to remember the ordering --
// and does it filter on True or False?
filter(lambda x: x % 2 == 0, range(10)) --> 0 2 4 6 8
// The list comprehension is self documenting
[x for x in range(10) if x % 2 == 0] --> 0 2 4 6 8
I can also convert the majority of list comprehensions into English quite easily -- "output x for each x in range(10) if x is divisible by 2" (though it may be cleaner if it was "for each x in range(10) [output x] if x is divisible by 2").[1]: http://php.net/quickref.php
[2]: http://docs.python.org/library/functions.html + http://docs.python.org/library/exceptions.html
They could do what Python did for 3.0, and make a tool similar to Python's 2to3 that replaces global namespace references.