From: gentlestone on 30 Mar 2010 11:40 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?
From: Mike Kent on 30 Mar 2010 11:44 On Mar 30, 11:40 am, gentlestone <tibor.b...(a)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? return ('Yes' if a == b else 'No')
From: Chris Rebert on 30 Mar 2010 12:01 On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.beck(a)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. Cheers, Chris -- http://blog.rebertia.com
From: Daniel Fetchinson on 30 Mar 2010 12:17 >> 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? > > return ('Yes' if a == b else 'No') And for less clutter you can even leave the parenthesis: return 'Yes' if a == b else 'No' -- Psss, psss, put it down! - http://www.cafepress.com/putitdown
From: John Nagle on 30 Mar 2010 13:08
Chris Rebert wrote: > On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.beck(a)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. John Nagle |