Some syntactic sugar proposals

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Nov 18 19:09:37 EST 2010


On Thu, 18 Nov 2010 09:32:23 +0000, Mark Wooding wrote:

[...]
> You're wrong.  Python evaluates these left-to-right, as I said.
> Parentheses override operator associativity; they don't affect
> evaluation order at all.

Fair enough. I concede your point.


[...]
>> Not everything needs to be a one liner. If you need this, do it the
>> old- fashioned way:
>>
>> t = foo()
>> if not pred(t): t = default_value
> 
> I already explained how to write it as a one-liner:
> 
>         t = (lambda y: y if pred(y) else default_value)(foo())


I didn't say it couldn't be written as a one-liner. I suggested that it 
was better not to.

The costs of the one-liner are:

* reduced readability;
* requires an increased level of knowledge of the reader ("what's lambda 
do?");
* runtime inefficiency (you create a function object, only to use it once 
then throw it away).

The advantages?

* one fewer line of code.

In my experience, the obsessiveness in which some people look for one-
liners is far from helpful, and goes against the spirit of Python. This 
isn't Perl :)


-- 
Steven



More information about the Python-list mailing list