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

MRAB python at mrabarnett.plus.com
Fri Apr 22 21:00:00 CEST 2011


On 22/04/2011 19:44, Bruce Leban wrote:
>
>
> On Fri, Apr 22, 2011 at 10:41 AM, Sturla Molden <sturla at molden.no
> <mailto: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
>
>     which presumably should mean
>
>          if cond: a = 'orange'
>
>
> I don' think that having this as part of the tuple/list constructor
> means that it "should be legal to write" everywhere else in the
> language. Would you also expect:
>
>     f(a, b if c, d) == f(a, b, d) if c else f(a, d)
>
Isn't the argument list of 'f' a tuple?

>     if foo if bar: == if bar: if foo:
>
>     return foo if bar == if bar: return foo
>
I'm having a bit of trouble parsing those...

>
> I fail to see an advantage in these while I do see that it would be
> useful to write:
>
>     (a if x,
>     b if y,
>     c if z,
>     ...)
>
>
> If this works for tuple constructors, it *does* seem to me that it
> should work for lists, sets and dicts. That last one is sticky is it:
>
>     { a if x : 1 }
>     { a : 1 if x }
>
The second one, because both the key and the value are conditional.

> or something else? Maybe tuples, lists and dicts are enough. Or maybe
> this just isn't useful enough. I'm +0.1 on this.
>



More information about the Python-ideas mailing list