Re: Ternary Operator in Python

2005-04-02 Thread Sunnan
Robert Kern wrote:
Sunnan wrote:
(((0.0  a)  1.0)  b )  2.0

Go on. Try it with a bunch of different values.
My bad. (Of course. The subexpressions must return booleans, not the 
largest number. It couldn't work any other way.) Egg on my face, and all 
that (figuratively speaking).

Not used to infix...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-02 Thread Scott David Daniels
Roy Smith wrote:
...  How our tools warp our thinking.
That is what it means to be human.  I can think of no better reason
for a programmer to regularly learn languages: our tools warp our
thinking.  A programmer is a professionally warped thinker.
--Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-02 Thread Steven Bethard
Scott David Daniels wrote:
Roy Smith wrote:
...  How our tools warp our thinking.
That is what it means to be human.  I can think of no better reason
for a programmer to regularly learn languages: our tools warp our
thinking.  A programmer is a professionally warped thinker.
--Scott David Daniels
[EMAIL PROTECTED]
+1 QOTW
STeVe
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Sean Kemplay
You could use 

condition and consequent or alternative

I use it
Sean

On Apr 1, 2005 5:24 PM, praba kar [EMAIL PROTECTED] wrote:
 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
 
 
 __
 Yahoo! Messenger
 Show us what our next emoticon should look like. Join the fun.
 http://www.advision.webevents.yahoo.com/emoticontest
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Steven Bethard
praba kar wrote:
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.
http://www.python.org/peps/pep-0308.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Erik Max Francis
Sean Kemplay wrote:
You could use 

condition and consequent or alternative
I use it
You should do so cautiously, since if consequent is false, it will not 
behave as suspected.  Not to mention that it's quite unreadable.

--
Erik Max Francis  [EMAIL PROTECTED]  http://www.alcyone.com/max/
San Jose, CA, USA  37 20 N 121 53 W  AIM erikmaxfrancis
  Physics, as we know it, will be over in six months.
  -- Max Born (1928)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Sean Kemplay
On Apr 1, 2005 8:10 PM, Erik Max Francis [EMAIL PROTECTED] wrote:
 Sean Kemplay wrote:
 
  You could use
 
  condition and consequent or alternative
 
  I use it
 
 You should do so cautiously, since if consequent is false, it will not
 behave as suspected.  Not to mention that it's quite unreadable.
 
 --
 Erik Max Francis  [EMAIL PROTECTED]  http://www.alcyone.com/max/
 San Jose, CA, USA  37 20 N 121 53 W  AIM erikmaxfrancis
Physics, as we know it, will be over in six months.
-- Max Born (1928)
 --
 http://mail.python.org/mailman/listinfo/python-list
 

I should have mentioned that, as I have been caught out before.

Sean
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Roy Smith
praba kar [EMAIL PROTECTED] wrote:

 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.

You answered your own question; there is no ternary operator in Python.  
There was a major debate on this newsgroup a year or so ago on this 
subject, and the decision was quite clear that no such feature would be 
added.

If you google for python ternary, you will find a huge amount of material 
written on the subject.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread John Roth
praba kar [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
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.
There are a number of workarounds; the most
used one seems to be based on a feature of the
'and' and 'or' operators. I believe Pep 308 has
a summary of the different ways you can do it,
and the advantages and drawbacks of each.
John Roth

__
Yahoo! Messenger
Show us what our next emoticon should look like. Join the fun.
http://www.advision.webevents.yahoo.com/emoticontest 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Diez B. Roggisch
praba kar wrote:

 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 is no ternary operator in python. There are several idioms that can be
used to emulate one to a certain degree - but they are scolwed on by quite
a few people. So better to not use them and just do it in a if: else:
clause. 

-- 
Regards,

Diez B. Roggisch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Scott David Daniels
John Roth wrote:
praba kar [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]

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
[EMAIL PROTECTED]
-- No truth has been harmed by this April Fool's post. :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Ron_Adam
On Fri, 1 Apr 2005 08:24:42 +0100 (BST), praba kar
[EMAIL PROTECTED] wrote:

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


   
__ 
Yahoo! Messenger 
Show us what our next emoticon should look like. Join the fun. 
http://www.advision.webevents.yahoo.com/emoticontest


I've used boolean opperations to do it.

result = (v == value) * first + (v != value) * second

Same as:

if v == value: result = first else: result = second


Ron

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Terry Reedy

praba kar [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 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

A unary operator has one operand; a binary operator has two operands; a 
ternary operator has three operands.  Python has none built-in, although 
one can 'synthesize' all sorts of ternary operators by combining two binary 
operators, with the operand of one being the result of the other.  However, 
people often don't think of such combinations as being such.

Since C has one builtin ternary 'operator' (if-else), the term 'ternary 
operator' is too often used as a synonym for that particular example of a 
ternary operator.  Others have referred you to discussions of if-else 
expressions in Python.

Ironically, if one thinks of or defines an operator as being a function 
called without parenthesis, then C's ';:' and Python's 'and..or' are not 
really ternary operators but flow control expressions equivalent in effect 
to certain if/else statement pairs.  That is because some of the operands 
may not be evaluated.  That is also why there are no special methods 
corresponding to 'and' and 'or'.  They are directly compiled to conditional 
code with no a function call corresponding to the 'operator'.

Terry J. Reedy



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Erik Max Francis
Ron_Adam wrote:
I've used boolean opperations to do it.
result = (v == value) * first + (v != value) * second
Same as:
	if v == value: result = first else: result = second
No, it isn't, because it isn't short circuiting.  If first or second had 
side effects, then the two would not be equivalent.

--
Erik Max Francis  [EMAIL PROTECTED]  http://www.alcyone.com/max/
San Jose, CA, USA  37 20 N 121 53 W  AIM erikmaxfrancis
  If the sun comes up / And you're not home / I'll be strong
  -- India Arie
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Carl Banks

Terry Reedy wrote:
 praba kar [EMAIL PROTECTED] wrote in message
 news:[EMAIL PROTECTED]
  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

 A unary operator has one operand; a binary operator has two operands;
a
 ternary operator has three operands.  Python has none built-in,

Not so fast, my friend.  What about the expression 0.0  a  1.0?


-- 
CARL BANKS

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Terry Reedy

Carl Banks [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 A unary operator has one operand; a binary operator has two operands;
 ternary operator has three operands.  Python has none built-in,

 Not so fast, my friend.  What about the expression 0.0  a  1.0?

Gee, what about 0.0  a  1.0  b  2.0?  I see both as synthesized 
multinary operators, but your are right in that this combination does act 
differently than a+b+c.

Terry J. Reedy




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Roy Smith
In article [EMAIL PROTECTED],
 Carl Banks [EMAIL PROTECTED] wrote:

 Terry Reedy wrote:
  praba kar [EMAIL PROTECTED] wrote in message
  news:[EMAIL PROTECTED]
   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
 
  A unary operator has one operand; a binary operator has two operands;
 a
  ternary operator has three operands.  Python has none built-in,
 
 Not so fast, my friend.  What about the expression 0.0  a  1.0?

I still remember one of the earliest bugs I ever wrote (I've long since 
forgotten most of the zillions I've written since).  It must have been 
around 1975, and my high school had an ASR-33 connected to a HP-3000 
running Time Shared Basic at another school a few towns away.

I wrote something like 1  X  10 and got an error.  I was puzzled by 
this, since we were using this notation in math class.  The answer of 
course was that I needed to write 1  X AND X  10, which I found really 
annoying and strange looking.  Or is my long-term memory returning 
corrupted data?  Maybe BASIC let you do 1  X  10, but I ran into this 
when I moved onto FORTRAN the next year?

In any case, I've gotten so used to writing 1  x  x  10 (or variations 
on the theme) that now I've got a language which lets me write it the 
normal math way, 1  x  10, and *that* looks strange.  Wierd, huh?  How 
our tools warp our thinking.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Carl Banks

Terry Reedy wrote:
 Carl Banks [EMAIL PROTECTED] wrote in message
 news:[EMAIL PROTECTED]
  A unary operator has one operand; a binary operator has two
operands;
  ternary operator has three operands.  Python has none built-in,
 
  Not so fast, my friend.  What about the expression 0.0  a  1.0?

 Gee, what about 0.0  a  1.0  b  2.0?  I see both as synthesized
 multinary operators, but your are right in that this combination does
act
 differently than a+b+c.

It seems that Python has an infinite number of operators.


-- 
CARL BANKS

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread gene . tani
The good ol' DiveInto says:

http://diveintopython.org/power_of_introspection/and_or.html#d0e9975

http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52310

Diez B. Roggisch wrote:
 praba kar wrote:

  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 is no ternary operator in python. There are several idioms that
can be
 used to emulate one to a certain degree - but they are scolwed on by
quite
 a few people. So better to not use them and just do it in a if: else:
 clause. 
 
 -- 
 Regards,
 
 Diez B. Roggisch

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Sunnan
Terry Reedy wrote:
Gee, what about 0.0  a  1.0  b  2.0?  I see both as synthesized 
multinary operators, but your are right in that this combination does act 
differently than a+b+c.
Is  really multinary in python? It looks binary to me, just like +.
(a+b)+c
(((0.0  a)  1.0)  b )  2.0
Sunnan
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary Operator in Python

2005-04-01 Thread Robert Kern
Sunnan wrote:
Terry Reedy wrote:
Gee, what about 0.0  a  1.0  b  2.0?  I see both as synthesized 
multinary operators, but your are right in that this combination does 
act differently than a+b+c.

Is  really multinary in python? It looks binary to me, just like +.
(a+b)+c
(((0.0  a)  1.0)  b )  2.0
Go on. Try it with a bunch of different values.
--
Robert Kern
[EMAIL PROTECTED]
In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die.
  -- Richard Harter
--
http://mail.python.org/mailman/listinfo/python-list