Is there something similar to ?: operator (C/C++) in Python?
Andrew Durdin
adurdin at gmail.com
Wed Jun 29 02:27:42 EDT 2005
On 6/29/05, Scott David Daniels <Scott.Daniels at acm.org> wrote:
> result = [(lambda: expr0), lambda: expr1][cond]()
Which still has an error, as evidenced by the following:
>>> cond = ""
>>> result = [(lambda: expr0), lambda: expr1][cond]()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list indices must be integers
Corrected version:
result = [(lambda: expr0), lambda: expr1][bool(cond)]()
Or if using python < 2.3:
result = [(lambda: expr0), lambda:
expr1][__import__("operator").truth(cond)]()
Andrew
More information about the Python-list
mailing list