substitute for c/java's ?:

Roman Suzi rnd at onego.ru
Thu Jun 14 03:17:20 EDT 2001


On 14 Jun 2001, Remco Gerlich wrote:

> Jochen Riekhof <jochen at riekhof.de> wrote in comp.lang.python:
> > Hi...
> > 
> > as a newcomer to python (like it a lot!) I found almost everything great
> > except two things:
> > 1. (most important)
> > I am missing something like the c/Java ?: operator. This is so convenient in
> > many places and saves a lot of typing.
> > It works like
> > result = option ? value1 : value2;
> > which is equivalent to
> > if option:
> >     result = value1
> > else:
> >     resultl = value2
> > 
> > Is there anything I overlooked?
> 
> Not really. Just write the if:, it's more obvious what it does when you read
> the code (especially when the expressions get longer).

result = option and value1 or value2

could also be used but only in those cases when value1
is TRUE.

For example:

result = DBG and "debug:" or ""

But this will not work:

result = NODBG and "" or "debug:"

(I`m not sure how to call these constructions, though).

Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 





More information about the Python-list mailing list