On Wed, 14 Dec 2005 21:16:23 +0100 in comp.lang.python, Lawrence Oluyede <[EMAIL PROTECTED]> wrote:
>Il 2005-12-14, Andy Leszczynski <[EMAIL PROTECTED]> ha scritto: >> How can do elegantly in Python: >> >> if condition: >> a=1 >> else: >> a=2 >> >> like in C: >> >> a=condition?1:2 >> > >There are tons of threads on this newsgroup and in the python-dev mailing >list about a ternary operator. There's also a PEP AFAIK. > >I like this: > >In [1]:switch = True > >In [2]:a = (1, 2)[switch] > >In [3]:print a >2 Note, however, you have the logic backwards. To duplicate the functionality of the OP's example, you need a = (2,1)[condition] or a = (1,2)[not condition] Regards, -=Dave -- Change is inevitable, progress is not. -- http://mail.python.org/mailman/listinfo/python-list