[Python-ideas] Accessing the result of comprehension's expression from the conditional
average
dreamingforward at gmail.com
Sat Jun 20 20:11:49 CEST 2009
From: Lie Ryan wrote:
> In list/generator comprehension, currently we have no way to access the
> result of the expression and have to write something like this:
>
> [f(x) for x in l if f(x) > 0]
>
> if f() is heavy or non-pure (i.e. have side effects), calling f() twice
> might be undesirable.
>
> I'm suggesting about something like:
>
> [f(x) for x in l if @ > 0]
>
> where @ is the result of the listcomp's expression (i.e. f(x))
>
> Personally, I don't like the use of symbols like @, as python is not
> perl. I'm still thinking of a better syntax/approach and is open for
> suggestion.
>
> What do you guys think?
I think it's a fine idea. The English language has evolved such a
construct, typically used when one wants to highlight (rather than
simply *refer* (as a pronoun does) to) the dominant noun without
resorting to redundancy.
[f(x) for x in l if same > 0] OR another possibility
[f(x) for x in l if self > 0]
obviously syntax highlighting makes this (or any keyword) standout
better. Would be interesting to consider other usages for such a
keyword addition (in the case of the former).
Aahz:
> Listcomps and genexps are like lambdas: run up against their limits and
> you should switch to a regular for loop or generator.
Generally decent advice, but there is value to exploring the
Kolomogorov optimums for a given expressive need (compressing the
highest amount of meaning in the simplest construction). In fact, I
consider Python to be one of best languages in this regard (it's
syntax being the evolution of programming-expressive compactness while
being complete in itself. (In contrast to Perl, for example, which
offloads the complexity to the programmer.)
mark
More information about the Python-ideas
mailing list