Ternary Operator in Python

Scott David Daniels Scott.Daniels at Acm.Org
Fri Apr 1 11:27:49 EST 2005


John Roth wrote:
> 
> "praba kar" <prabapython at yahoo.co.in> wrote in message 
> news:mailman.1172.1112340286.1799.python-list at python.org...
> 
>> Dear All,
>>    I am new to Python.  I want to know how to
>> work with ternary operator in Python.  I cannot
>> find any ternary operator in Python.  So Kindly
>> clear my doubt regarding this
> 
> 
> There isn't one, and there won't be one unless Guido
> changes his mind, and that's quite unlikely.

Au contraire, mon frere:

There is a ternary operator in Python (fairly ill-documented)
Its name is "partial polynomial eval."  As is traditional in
implementing ternary operations in computer languages, the
name of the operator does not show up anywhere in the code,
so many people don't realize they are using it.  Here is an
example of the "ppe" used to evaluate a cubic, using the
ternary operator three times in a single statement:

     def cubic(x, a, b, c, d):
         return ((a * x + b) * x + c) * x + d

As you may be able to guess, the more common name is "*+",
and it is a nice self-documenting operator (it behaves just
like the primitives).  Originally the DEC Vax provided this
as a "vectorized" opcode, which is where Python got the
idea.  You probably don't see it since you aren't doing much
engineering work.

--Scott David Daniels
Scott.Daniels at Acm.Org

-- No truth has been harmed by this April Fool's post. :-)



More information about the Python-list mailing list