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

Bruce Leban bruce at leapyear.org
Fri Apr 22 20:44:04 CEST 2011


On Fri, Apr 22, 2011 at 10: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
>
> 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)

if foo if bar: == if bar: if foo:

return foo if bar == if bar: return foo


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 }

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

--- Bruce
*New! *Puzzazz newsletter: http://j.mp/puzzazz-news-2011-04 including April
Fools!
*New!** *Blog post: http://www.vroospeak.com Ironically, a glaring Google
grammatical error
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110422/e35c20e0/attachment.html>


More information about the Python-ideas mailing list