PEP-308 a "simplicity-first" alternative
Chris Liechti
cliechti at gmx.net
Tue Feb 11 18:37:24 EST 2003
holger krekel <pyth at devel.trillke.net> wrote in
news:mailman.1044992059.13191.python-list at python.org:
> I am still wondering if there is a simple way to fix the current
> ternary op "x and y or z". Everybody knows by now that
> this "fails" if y is a false value. Otherwise
> it works ok and is used in today's code everywhere.
>
> Inspired by "do the simplest thing that can possibly work"
> i now think that
>
> x and y else z
>
> might just do it and avoid the need for a new construct.
> It's a very minor change just for fixing the problem at hand.
> It should be obvious what it does.
pro:
'else' is already used in various places (even where a C programmer would
not expect it: for, while, try..except ;-)
con:
what is 'else' exactly doing here?
at first glance it returns y if y is not False else z (oops i just wrote
Guidos syntax ;-)
but then, it still fails if y is a function that returns False...
so how should that be implemented? i guess the parser would have to match
the whole "and else" construct, so "or else" would generate a SyntaxError?
> if 'x' is true then 'y' is the result. Else 'y' is the
> result. So it doesn't matter if 'y' has a false value.
> Yes, it looks unusal but it is a simple "non-destructive"
> change which the people who now dislike PEP308 could
> probably live with.
i strongly dislike many of the other 'solutions' discussed. no ':<>-' for
me please. for those who like that, there is
http://www.muppetlabs.com/~breadbox/bf/ <wink>
i find Guidos sugestion appealing but i share your concerns about silly 'if
y if c else z: ...' constructs... but still it's one of the best solutions
if we need one.
> It has the positive side effect that explaining this
> ternary op variation goes hand in hand with explaining
> how and/or works in python.
hm... see my comment above. how do you explain what 'else' exactly does
here?
i agree that it is a good opportunity to learn about short circuiting with
'and' and 'or' as well as the non-boolean outcome of these ops.
> And it acknowledges the
> fact that we "almost" have a ternary op. Also it reads
> left to right and the alternatives "y else z"
> read well.
IMHO it's definetly easy to read and would fit it.
chris
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list