46

Possible Duplicate:
Python Ternary Operator

I want to print out a string in python. I don't want to do:

if isfemale_bit:
    print 'F'
else:
    print 'M'

The best I have right now is print ['M', 'F'][int(isfemale_bit)]?

Is there a better alternative?

I needs my syntactic sugar!!

Community
  • 1
  • 1
engineerchuan
  • 2,292
  • 3
  • 20
  • 20
  • 1
    No, you need the clearest, most obvious, and most maintainable code possible. Why do you want to emulate an operator that's little better than an obfuscation mechanism? – Nicholas Knight May 13 '11 at 17:09
  • I like your original solution but ('M', 'F')[int(isfemale_bit)] is the optimal version. You should be able to give kudos for crafty solutions like yours. – freegnu May 13 '11 at 17:36
  • 3
    @freegnu: wrong; you should opt for clarity over clever-hackery every time. – Katriel May 13 '11 at 18:29
  • 2
    Maybe you could take some time out to explain what part of the above statement is unclear. It is easy to read and succinctly performs the desired operation an is valid Python. Maybe I'm reading it wrong. I'm always willing to learn something new. Would you care to back up your opinion and authoritative stance with an example or two where this fails? Or maybe you should take your crusade against succinct code as hackerish witchcraft to the perl section where there is more fuel for the fire. – freegnu May 17 '11 at 04:46

4 Answers4

57

In Python 2.5, you can use ternary conditionals like this:

a if b else c

There is more discussion here: Does Python have a ternary conditional operator?

Community
  • 1
  • 1
Paul Rosania
  • 9,083
  • 2
  • 18
  • 18
16

Ah the ternary operator:

>>> print 'foo' if True else 'bar'
foo
>>> print 'foo' if False else 'bar'
bar
senderle
  • 125,265
  • 32
  • 201
  • 223
14
 print 'F' if isfemale_bit else 'M'
Stefano Borini
  • 125,999
  • 87
  • 277
  • 404
9

I guess you are looking for a solution similar o isfemale_bit?'F':'M' in C code So you can use the and-or construction (see Dive Into Python)

print isfemale_bit and 'F' or 'M'

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
ThibThib
  • 7,270
  • 3
  • 27
  • 36
  • If `and` evaluates to true, it returns its second argument; `or` returns its last truthy argument. You can combine these properties to get the equivalent of the ternary operator. A little difficult to read, but it works. – Paul Rosania May 13 '11 at 17:19
  • 4
    @Paul Rosania, I understand how it works. What I don't understand is why you would do this, ever. – senderle May 13 '11 at 17:27
  • Learn to think in Python, the whole language, before you poo poo the real syntax of the language. Long winded statements in Python just show the inexperience of the programmer. i.e. You haven't started thinking in Python. – freegnu May 13 '11 at 17:33
  • @freegnu, I'm not poo-pooing any syntax -- I'm poo-pooing a use case. Are you calling the ternary operator long winded? Are you saying this is a better option? Still, I'll withdraw my snarky question marks -- I was being a bit rude -- sorry! :) – senderle May 13 '11 at 17:51
  • 2
    This was common before python had a ternary operator (its fairly new), but no longer really makes sense any more. If you want the ternary to return falsy values, this breaks in a very difficult-to-debug way if you're viewing it as a ternary operator. This is also much more difficult to read than the real ternary. I would advise strongly against it. – Aaron Dufour May 13 '11 at 19:27
  • Honestly what will you do as the language evolves. Sticking you head in the ground and saying the new language elements are unclear is not helpful. Authoritatively saying the new language elements are unclear, confusing, or unsafe is as ridiculous as it is lame. I'd suggest you learn the new language elements and stop asking others to ignore and shun the perfectly valid, safe, tested, and clearly understandable parts of the language that you may not have been aware of. – freegnu May 17 '11 at 04:29