Proposal: function which simulates C ?: operator

Mitja nun at example.com
Sun Jul 11 09:52:48 EDT 2004


Adal Chiriliuc <me at spammers.com>
(news:1388851004.20040711153354 at smtp.myrealbox.com) wrote:
> I think a function similar to the one below should be
> added to the
> builtin module:
>
> def boolselect(condition, trueresult, falseresult):
>     if condition:
>         return trueresult
>     else:
>         return falseresult
>
> --- WITH
>
> scalewidth = boolselect(self.__vertical, _scalew, _scaleh)
>
> --- WITHOUT
>
> if self.__vertical:
>     scalewidth = _scalew
> else:
>     scalewidth = _scaleh

> What do you think?

Here's a nifty workaround I figured out myself but am sure others use it as
well:
scalewidth=(_scaleh,_scalew)[__self.vertical]

HTH

It's not as readable as the "if cond ? true_expr : false_expr", but I think
introducing syntax like this is far from needed and pretty complicated.





More information about the Python-list mailing list