[Python-ideas] Where-statement (Proposal for function expressions)
Ben Finney
ben+python at benfinney.id.au
Sat Jul 18 01:44:25 CEST 2009
Gerald Britton <gerald.britton at gmail.com>
writes:
> Anyway, I like the "where" idea not because of real or imagined
> performance gains, but because of its cleanness when expressing
> problems.
(Speaking of cwclean expression, please don't top-post; instead, remove
irrelevant quoted material and respond beneath each remaining point
<URL:http://en.wikipedia.org/wiki/Posting_style#Inline_replying>.)
> A big use for me would be in list comprehensions. In one
> project I work on, I see things like:
>
> for i in [item in self.someobject.get_generator() if self.important_test(item)]
>
> and other really long object references.
>
> which I would like to write:
>
> mylist = [item in f if g(item)] where:
> f = self.someobject.get_generator()
> g = self.important_test
I presume these two are supposed to be equivalent (and syntactically
valid), so I'll take it the first one should be something like::
mylist = [item for item in self.someobject.get_generator() if self.important_test(item)]
> To my eyes, the first is harder to read than the second one.
That's largely attributable to the fact that you've got one alternative
all on a single line, and the other broken into more easily-readable
lines. I don't think the ‘where’ syntax is much help there.
I would write the single-statement version as::
mylist = [
item for item in self.someobject.get_generator()
if self.important_test(item)]
which makes it far more readable. I argue that this does away with
pretty much any justification for your use case above.
--
\ “… a Microsoft Certified System Engineer is to information |
`\ technology as a McDonalds Certified Food Specialist is to the |
_o__) culinary arts.” —Michael Bacarella |
Ben Finney
More information about the Python-ideas
mailing list