Tuesday, 21 May 2013

Python - Shortcut Conditional Expressions

If...Else Statement in one line

foo = 1
A = 'A'
B = 'B'
C = 'C'

x = A if foo > 0 else B
# A
 x = lambda: A if foo < 0 else B
x()
# B
x = A if not foo else B if foo < 0 else C
# C
Source: http://www.python.org/dev/peps/pep-0308/

No comments :

Post a Comment