For review: PEP 308 - If-then-else expression
John La Rooy
nospampls.jlr at doctor.com
Wed Feb 12 01:47:48 EST 2003
On Wed, 12 Feb 2003 15:27:13 +1100
Andrew McNamara <andrewm at object-craft.com.au> wrote:
> >>Piet van Oostrum wrote:
> >> ...
> >> If b is a variable that takes only 0 and 1 as values then
> >>
> >> b -> (x, y) is the same as
> >> b -> [y, x]
> >>
> >> because with boolean conditionals people are used to have the true clause
> >> first. And we shouldn't change that.
> >
> >Argh -- I had missed that. OK, then I don't like the proposal any
> >more -- having to switch order of x and y depending on what
> >parentheses you use would make the construct unusable IMHO, and,
> >without the generality of being able to use it in the integer
> >case, I don't think the usefulness is sufficient to warrant a
> >new operator. Pity:-(.
>
> Half baked idea warning - what about:
>
> Boolean:
>
> c -> { True: x, False: y }
>
> Int:
>
> c -> { 0: x, 1: y, 2: z }
>
> --
> Andrew McNamara, Senior Developer, Object Craft
> http://www.object-craft.com.au/
>
Isn't this similar to
{ True: x, False: y }[c]
or
{ 0: x, 1: y, 2: z }[c]
Presumably the alternate syntax is to allow lazy evaluation of the dictionary.
How about a way to specify a default value in the cases which you don't wish to
or can't fully enumerate (int for instance)? Like
{ 0: x, 1: y, 2: z }.get(c,default)
all that's missing here is the short circuiting which can of course be done with
lambda, but then we lose the consiseness
John
More information about the Python-list
mailing list