[Python-ideas] ('blue', 'red', 'orange' if something, 'green')

Eric Snow ericsnowcurrently at gmail.com
Fri Apr 22 21:07:05 CEST 2011


On Fri, Apr 22, 2011 at 11:41 AM, Sturla Molden <sturla at molden.no> wrote:

> Den 22.04.2011 11:59, skrev cool-RR:
>
>> Here's an idea that would have helped me today while coding. Allow
>> something like this:
>>
>>    ('blue', 'red', 'orange' if some_condition, 'green')
>>
>> So 'orange' is included in the tuple only if `some_condition` evaluates to
>> `True`.
>>
>
> This means it should be legal to write
>
>    a = 'orange' if cond
>
>
>
More accurately:

(a = 'orange') if cond

and such conditional assignment statements don't exist in python.


> which presumably should mean
>
>     if cond: a = 'orange'
>
> It retains some symmetry with
>
>    a = 'orange' if cond else 'yellow'
>
>
The symmetry is with the following:

a = 'orange' if cond else None

Though the else clause with None would be implicit.  This would result in
"a" getting set to one or the other.  However, the original idea implied
that the "a" should not be be set at all if cond is true, as results in your
example above with the if statement.

I am not sure about the feasibility of adding conditional execution clauses
to statements.  And then embedding a related conditional composition in
expression lists (for argument lists, etc.)...  I'm not sure it's worth it.


-eric


> Sturla
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110422/3bceebc5/attachment.html>


More information about the Python-ideas mailing list