My .02 on PEP308 + an extra question.

Andrew Dalke adalke at mindspring.com
Mon Feb 10 16:16:15 EST 2003


Max M:
> What about:
>
> iif(x==y, this_takes_a_second(), this_takes_an_hour(aVal=42))
>
> In your case both the functions will be executed allways. The idea about
> the ternary operator is that only the selected function will be called.
>
> That is why a language construct is nessecary.

No, it is not.  Short-circuiting is great in theory, but is rarely
used in practice.  I've given estimates elsewhere based on code
analysis which suggests that

  - an approprate if/else (either expression or statement) is used
       about once every 400 lines (or more often; this is a guess
       based on data and not a direct result of analysis))

  - an operator based version would be used about 1 in 1000
      lines of Python code (based on code analysis, the numbers
       range from about 400 lines to about 2,000 lines, with 1000
       being a good approximation)

  - a short-circuiting version is needed about once every 5,000
      lines of code

  - a short-circuiting version which cannot be handled by the
       cond and expr1 or expr2   hack occurs about once every
     25,000 lines (this is based on Tim Peter's scan of Python's
      standard library, but should be treated as a rough
      approximation)

So you are saying that a construct is necessary when it
must be used only once every 25,000 lines of Python code
(or once every 5,000 if you are feeling generous).

While an 'iff'/'ifelse'/'check' function is an apropriate solution
for the other 60 (or 12) times there is need for an if/else-like
construct.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list