the PHP ternary operator equivalent on Python

Paul Rubin http
Wed Nov 23 23:18:58 CET 2005


Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
> dNewDataFields['CODE'] = dOldDataFields['CODEDATA']
> dNewDataFields['DATE'] = dOldDataFields['DATE']
> if dOldDataFields['CONTACTTYPE'] == 2:
>     dNewDataFields['CONTACT'] = dOldDataFields['FIRSTCONTACT']
> else:
>     dNewDataFields['CONTACT'] = dOldDataFields['SECONDCONTACT']
> 
> There. The world didn't end. 

It gets messy for a more complicated structure:

    d = {'x': x1() if x_is_old else x2(),
         'y': y1() if y_is_old else y2(),
         'z': z1() if z_is_old else z2() }

etc.



More information about the Python-list mailing list