(a==b) ? 'Yes' : 'No'

Den patentsvnc at gmail.com
Thu Apr 1 11:27:53 EDT 2010


On Mar 30, 10:56 am, Steve Holden <st... at holdenweb.com> wrote:
> John Nagle wrote:
> > Chris Rebert wrote:
> >> On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.b... at hotmail.com>
> >> wrote:
> >>> Hi, how can I write the popular C/JAVA syntax in Python?
>
> >>> Java example:
> >>>    return (a==b) ? 'Yes' : 'No'
>
> >>> My first idea is:
> >>>    return ('No','Yes')[bool(a==b)]
>
> >>> Is there a more elegant/common python expression for this?
>
> >> Yes, Python has ternary operator-like syntax:
> >> return ('Yes' if a==b else 'No')
>
> >> Note that this requires a recent version of Python.
>
> >     Who let the dogs in?  That's awful syntax.
>
> Yes, that's deliberately awful syntax. Guido designed it that way to
> ensure that people didn't aver-use it, thereby reducing the readability
> of Python applications. Speaking purely personally I hardly ever use it,
> but don't dislike it.
>
> regards
>  Steve
> --
> Steve Holden           +1 571 484 6266   +1 800 494 3119
> See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
> Holden Web LLC                http://www.holdenweb.com/
> UPCOMING EVENTS:        http://holdenweb.eventbrite.com/

I've been following this thread for a few days now.  My thoughts are
that, in view of a long known widely used syntax for this operator,
python's syntax seems like change for change sake.  If current
programing paradigm provides that particular trinary operator, why
should python's be different from the previously well known one.

For instance, no reasonable language designer would, now, use post-fix
(I know about Forth) or allow only +=, -=, /=, etc. assignments ONLY.
(Just as no reasonable car designer would put the accelerator pedal on
the left.)  There are conventions which should span products.  Yes
python has the trinary operator and it's not going to change, but this
seems like a bit of petulance on the part of the designer.

Den



More information about the Python-list mailing list