PEP 308 - ternary operator

Nick Trout 1nick2trout3 at shaw.ca_AddedNumbers4AntiSpam
Sun Feb 16 20:12:31 CET 2003


> >My preference for an immediate-if operator is the traditional '?' from C:
> >
> >    cond ? expr1 : expr2
> >
>
> I'm not quite shure whether this is old news or not, but I haven't seen
> the following mentioned here.


Has anyone pointed out that your can do ternary operators using:

result = { 1: true_value, 0: false_value }[condition]

or

result = { 1: true_fn, 0: false_fn }[condition]()

if they are functions.

I dont expect this will be treated as a solution to this extensive debate,
but it is a solution.

Nick










More information about the Python-list mailing list