PEP308: Yet another syntax proposal
holger krekel
pyth at devel.trillke.net
Mon Feb 10 12:32:08 EST 2003
Harvey Thomas wrote:
> Unless I'm missing the point completely, compare:
>
> if cond:
> x = calc(1, resource_intensive_1())
> else:
> x = calc(1, resource_intensive_2())
>
> with
>
> x = calc(1, iif(cond, resource_intensive_1(), resource_intensive_2()))
>
> In the second case both resource_intensive_1() and resource_intensive_2()
> are evaluated. There is of course the possibility of undesired side-effects.
I don't mind having to write the 4-liner if i execute
"resource_intensive" functions. I would say that *especially* when
you have "huge" functions with side effects you should choose the 4-liner
over an expression. Often the two functions will have different
arguments where you need to adapt to. Adding this logic to the
already-too-long expressions becomes a pain to maintain and read.
far more often a ternary op is used to switch between two strings
or two numbers aka
result = input=='y' and "yes" or "no"
and has the net side effect that people (must) understand in their
learning process how and & or works in Python. Calling different
methods hints at conditional control-flow and that is better done
with an easy-to read if-statement which supports you by
indentation i.e. visual clues.
holger
More information about the Python-list
mailing list