On Tue, 2006-10-17 at 09:48, Tim Chase wrote: > [...] > Either of the following should suffice: > > # return a non-empty string > x is None and "None" or str(x)
This one can be "optimized" to just str(x) since str(None)=="None". >[...] > There are more baroque ways of writing the terniary operator in > python (although I understand 2.5 or maybe python 3k should have > a true way of doing this). Python 2.5 does already. > My understanding is that one common > solution is something like > > {True: "", False: str(x)}[x is None] As Fredrik pointed out in not so many words, this is not a good solution. Besides being ugly, the major problem with this solution is that both branches are evaluated regardless of the outcome of the condition. This is not good if the expression is unsafe to calculate under the wrong condition, or if the expressions are expensive to calculate, or if the expressions have side effects. The "condition and result1 or result2" hack at least prevents the evaluation of the non-applicable expression due to the short-circuiting nature of the "and" and "or" operators. -Carsten -- http://mail.python.org/mailman/listinfo/python-list