substitute for c/java's ?:

Jochen Riekhof jochen at riekhof.de
Wed Jun 13 12:03:16 EDT 2001


Hi...

> Instead I usually write:
> if option: result = value1
> else: result = value2
> just dropping the newlines. It's more verbose than
> result = option ? value1 : value2
> but at least it's clean.

Found a somewhat unclean solution that also has the drawback to process both
operands any way, but it is sometimes nice because short:
result (value1, value2)[not option]

It assumes that the only booleans emerging from not x are 1 and 0, I hope
this is defind in the language.

> There is no real equivalent of 'switch' in python.
> IIRC a few weeks ago there was a thread about using classes to
> get a similar effect.

Good idea! Thanks!

So, I just continue using python and wat for ?: to make its way into the
language (hinthint) ;-)

Ciao

...Jochen








More information about the Python-list mailing list