Python-list digest, Vol 1 #10572 - 14 msgs
Michael Gilfix
mgilfix at eecs.tufts.edu
Mon Apr 22 11:13:55 EDT 2002
On Mon, Apr 22 @ 09:51, Steve Holden wrote:
> I agree with this. But don't try to suggest that there aren't problems with
> the ternary construct in C, most notably the omission of an "=" in the test,
> turning a test into an assignment.
That's the whole point of the ternary operator isn't it? To make
binary decisions before an assignment. A useful example is a funtion
with mutli-directions:
void both_ways (int val, int direction)
{
another_func ( (direction) ? NULL : &val,
(direction) ? &val : NULL,
....);
}
That way you can easily switch arguments to another function without
more typing. But most people use the ternary operator as an if
statement, which is ugly.
While I'm not commenting on whether this is really useful in python
(because I get along fine without it), the python equiv would be:
def both_ways (arg, dir):
if dir:
arg1 = blah
arg2 = some_blah
else:
arg1 = something_else
arg2 = some_other_thing
another_func (arg1, arg2)
Yeah, I'm *sure* you can get fancier. I think I saw a PEP where
someone suggesting having if return a value. There have been
times when I think my syntax would have looked nicer/simpler/more
elegant/pick one if that were the case. I know it can be done
another way. But a bit of syntactic sugar isn't always a bad
thing.
-- Mike
--
Michael Gilfix
mgilfix at eecs.tufts.edu
For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html
More information about the Python-list
mailing list