[Python-ideas] ternary without else

Serhiy Storchaka storchaka at gmail.com
Fri May 25 06:33:05 EDT 2018


25.05.18 13:06, Jacco van Dorp пише:
> I would like to carefully suggest a half form of the ternary expression.
> 
> Currently, you can write code like:
> 
>>>> if cond:
>>>>     do_something
> 
> However, especially if the condition and action are both really
> simple, taking two lines feels like a bit of a waste. So I sometimes
> write:
> 
>>>> if cond: do_something
> 
> However, this isn't PEP8 compliant, and every linter complains about
> it. They'd be right if the condition and action were a bit more
> complicated.
> 
> I would very much like to write:
> 
>>>> do_something if cond
> 
> and be done with it. Like a ternary expression but without the else clause.

This isn't PEP8 compliant either.

I suppose that if this syntax be accepted by the compiler, it will be 
explicitly disallowed by PEP8 for the same reason as "if cond: 
do_something".

It is easier to pass an option to linter that will silence this warning 
than introduce a new ambiguous syntax.

For example try to interpret "[a for b if c]" if "b if c" be a valid 
expression.



More information about the Python-ideas mailing list