[Info] PEP 308 accepted - new conditional expressions

  • Thread starter Reinhold Birkenfeld
  • Start date
R

Reinhold Birkenfeld

Hi,

after Guido's pronouncement yesterday, in one of the next versions of Python
there will be a conditional expression with the following syntax:

X if C else Y

which is the same as today's

(Y, X)[bool(C)]

or

C and X or Y (only if X is True)

Reinhold
 
F

Fredrik Lundh

Reinhold said:
after Guido's pronouncement yesterday, in one of the next versions of Python
there will be a conditional expression with the following syntax:

X if C else Y

which is the same as today's

(Y, X)[bool(C)]

hopefully, only one of Y or X is actually evaluated ?
C and X or Y (only if X is True)

hopefully, "only if X is True" isn't in fact a limitation of "X if C else Y" ?

/... snip comment that the natural order is C, X, Y and that programmers that
care about readable code will probably want to be extremely careful with this
new feature .../

</F>
 
R

Reinhold Birkenfeld

Fredrik said:
Reinhold said:
after Guido's pronouncement yesterday, in one of the next versions of Python
there will be a conditional expression with the following syntax:

X if C else Y

which is the same as today's

(Y, X)[bool(C)]

hopefully, only one of Y or X is actually evaluated ?

(cough) Yes, sorry, it's not the same. The same would be

(C and lambda:X or lambda:Y)()

if I'm not much mistaken.
hopefully, "only if X is True" isn't in fact a limitation of "X if C else Y" ?

/... snip comment that the natural order is C, X, Y and that programmers that
care about readable code will probably want to be extremely careful with this
new feature .../

Yes, that was my comment too, but I'll not demonize it before I have used it.

Reinhold
 
R

Rocco Moretti

Reinhold said:
Hi,

after Guido's pronouncement yesterday, in one of the next versions of Python
there will be a conditional expression with the following syntax:

X if C else Y

Any word on chaining?

That is, what would happen with the following constructs:

A if B else C if D else F
A if B if C else D else F

The first one is the tricky bit - it could be either

(A if B else C) if D else F
or
A if B else (C if D else F)

I'd expect the former from left-> right semantics, but reading the
unparenthesized form, I'd see "A if B else ..." note that B is true, and
conclude the expression evaluated to A (which would be wrong if D is false).
 
D

Dave Benjamin

Reinhold said:
after Guido's pronouncement yesterday, in one of the next versions of Python
there will be a conditional expression with the following syntax:

X if C else Y

Hooray! After years of arguing over which syntax to use, and finally
giving up since nobody could agree, the Benevolent Dictator did what
only a dictator can do, and just made a damn decision already.

Thank you, Guido! =)

Dave
 
R

Reinhold Birkenfeld

Rocco said:
Any word on chaining?

That is, what would happen with the following constructs:

A if B else C if D else F
A if B if C else D else F

The first one is the tricky bit - it could be either

(A if B else C) if D else F
or
A if B else (C if D else F)

I'd expect the former from left-> right semantics, but reading the
unparenthesized form, I'd see "A if B else ..." note that B is true, and
conclude the expression evaluated to A (which would be wrong if D is false).

It will be

A if B else (C if D else F).

Quote:
"""
The priorities will be such that you can write

x = A if C else B
x = lambda: A if C else B
x = A if C else B if D else E

But you'd have to write

if (A if C else B):
[x for x in seq if (A if C else B)]
A if (X if C else Y) else B
(A if C else B) if D else E

Note that all these are intentionally ugly. :)
"""

Reinhold
 
R

Ron Adam

Reinhold said:
It will be

A if B else (C if D else F)

So this evaluates as if there are parentheses around each section.. Hmm?

(A) if (B) else ( (C) if (D) else (F) )

The first 'if' divided the expr, then each succeeding 'if' divides the
sub expressions, etc... ?

So ...

A if B else C + X * Y

Would evaluate as... ?

A if B else (C + X * Y)


and...

value = X * Y + A if B else C

would be ?

value = (X * Y + A) if B else C

or ?

value = X * Y + (A if B else C)



I think I'm going to make it a habit to put parentheses around these
things just as if they were required.

Cheers,
Ron
 
E

Erik Max Francis

Dave said:
Hooray! After years of arguing over which syntax to use, and finally
giving up since nobody could agree, the Benevolent Dictator did what
only a dictator can do, and just made a damn decision already.

Thank you, Guido! =)

Yes, hear hear.

So what made him change his mind? When the debates raged over PEP 308,
he seemed pretty dead set against it (at least by proxy) ...
 
S

Sam

Reinhold said:
Hi,

after Guido's pronouncement yesterday, in one of the next versions of Python
there will be a conditional expression with the following syntax:

X if C else Y

which is the same as today's

(Y, X)[bool(C)]

What's wrong with "C ? X:Y"?

Aside from ":" being overloaded?


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQBDPcH9x9p3GYHlUOIRAlVHAJ9zgyxICMojsZtKCimAzA8w0ILYvACfWqxv
iWsn3baqRydINp8eYjStR18=
=XWo0
-----END PGP SIGNATURE-----
 
J

Jaime Wyant

Reinhold said:
Hi,

after Guido's pronouncement yesterday, in one of the next versions of Python
there will be a conditional expression with the following syntax:

X if C else Y

which is the same as today's

(Y, X)[bool(C)]

What's wrong with "C ? X:Y"?

Aside from ":" being overloaded?

First thing that comes to my mind is that it is more C-ish (read
cryptic) than pythonic (read elegant and understandable).

jw
 
B

Bengt Richter

Fredrik said:
Reinhold said:
after Guido's pronouncement yesterday, in one of the next versions of Python
there will be a conditional expression with the following syntax:

X if C else Y

which is the same as today's

(Y, X)[bool(C)]

hopefully, only one of Y or X is actually evaluated ?

(cough) Yes, sorry, it's not the same. The same would be

(C and lambda:X or lambda:Y)()

if I'm not much mistaken.

I think you need to parenthesize, but also note that using lambda does not
always grab the currently-executing-scope X or Y, so I think the list container idiom
may be better to show the semantics of the new expression:

... C = True
... class K(object):
... X='this is K.X'
... Y='this is K.Y'
... cv = (C and (lambda:X) or (lambda:Y))()
... return K
... ... C = True
... class K(object):
... X='this is K.X'
... Y='this is K.Y'
... cv = (C and [X] or [Y])[0]
... return K
... 'this is K.X'

Yes, that was my comment too, but I'll not demonize it before I have used it.
Me too ;-)

Regards,
Bengt Richter
 
T

Terry Reedy

Dave Benjamin said:
Hooray! After years of arguing over which syntax to use, and finally
giving up since nobody could agree,

I understand that this has become the local 'politically correct' view, but
as a participant in the discussion, I know it not true and actively
deceptive. The community was prevented from coming to a decision. There
was a 'primary' vote with an incumbent, 15 challengers, and several
write-ins. But there was no runoff.

As recorded in http://www.python.org/peps/pep-0308.html, the incumbent
proposal, Guido's favorite from beginning to end,
x if C else y, was tied for 1st in rejections and was only 4th in
acceptances, being beaten by
(if C: x else: y) (which was also first in rejections)
C ? x : y
if C then x else y
Among the other proposals also, about 3/4ths had the order C x y.

I think if nothing else, the vote showed that the community pretty strongly
preferred the order C x y to x C y. I am pretty sure that a vote then (and
probably now) on that specific issue would have favored the first. But
that idea, along with any runoff among the best exemplars of each order,
was *rejected*, along with notice that 'unofficial' votes conducted by
community members would be ignored.

The discussion and decision process was also inhibited by the lack of any
ground rules as to what would be an acceptible candidate. So there was
lots of confusion and lots of time wasted on 'favorite son' candidates that
had no chance of winning (much as there once was on the first round of
voting at American presidential nominating conventions). There was also,
that I knew of, no definition of success nor a process aimed at success. I
doubt that many actually expected the obviously preliminary vote to reach a
definitive conclusion.

Even in the recent py-dev discussion, Guido declined, when I specifically
asked, to reveal his 'ballot' of alternatives he was considering. So what
were we actually supposed to discuss? Naturally, there followed irrelevant
noise posts on impossible alternatives and peripheral issues. And this
became a reason for Guido to 'just decide' on his original favorite, no
explanation offered.
----------

The lesson for me is to spend much less time on Python discussion and much
more on unfinished projects. So even if I never use the new syntax, I will
have gained something ;-)

Terry J. Reedy
 
S

Sam

Jaime said:
Reinhold said:
Hi,

after Guido's pronouncement yesterday, in one of the next versions of Python
there will be a conditional expression with the following syntax:

X if C else Y

which is the same as today's

(Y, X)[bool(C)]

What's wrong with "C ? X:Y"?

Aside from ":" being overloaded?

First thing that comes to my mind is that it is more C-ish (read
cryptic) than pythonic (read elegant and understandable).

And "foo if bar" is Perl-ish; yet, even Perl has the ? : operators.



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQBDPgkyx9p3GYHlUOIRAtYJAJ94v5hK3gZlSGTILzXguQNs4ct2dQCfZT7V
ZNafvlzn6LAlelGerLV9vH8=
=xsEg
-----END PGP SIGNATURE-----
 
M

Michele Simionato

Terry Reedy ha scritto:
I understand that this has become the local 'politically correct' view, but
as a participant in the discussion, I know it not true and actively
deceptive. The community was prevented from coming to a decision. There
was a 'primary' vote with an incumbent, 15 challengers, and several
write-ins. But there was no runoff.
<snip history of PEP 308>

FWIW, I think Terry's recollection is pretty close to the "historical
truth".
Guido could have decided two years ago, sparing us the PEP 308 ordalia.
So, I am happy that at the end we will have a conditional operator, but
I
am not happy of how the process worked out. It was just an enormous
waste
of resources that could have been employed much better :-(

Michele Simionato
 
R

Reinhold Birkenfeld

Ron said:
So this evaluates as if there are parentheses around each section.. Hmm?

(A) if (B) else ( (C) if (D) else (F) )

The first 'if' divided the expr, then each succeeding 'if' divides the
sub expressions, etc... ?

So ...

A if B else C + X * Y

Would evaluate as... ?

A if B else (C + X * Y)


and...

value = X * Y + A if B else C

would be ?

value = (X * Y + A) if B else C

or ?

value = X * Y + (A if B else C)



I think I'm going to make it a habit to put parentheses around these
things just as if they were required.

Yes, that's the best way to make it readable and understandable.

Reinhold
 
R

Reinhold Birkenfeld

Erik said:
Yes, hear hear.

So what made him change his mind? When the debates raged over PEP 308,
he seemed pretty dead set against it (at least by proxy) ...

Raymond Hettinger proposed to change the behavior of and/or to return one
of their arguments because he had been bitten by the common trap of
C and X or Y when X is False.

People were positive about it, but said that then a conditional would have
to be introduced. So Guido considered it again. He says himself that the
greatest problem last time was that no consensus over syntax was reached,
and he solved the problem this time by deciding himself.

Reinhold
 
R

Reinhold Birkenfeld

Sam said:
Jaime said:
Reinhold Birkenfeld writes:

Hi,

after Guido's pronouncement yesterday, in one of the next versions of Python
there will be a conditional expression with the following syntax:

X if C else Y

which is the same as today's

(Y, X)[bool(C)]

What's wrong with "C ? X:Y"?

Aside from ":" being overloaded?

First thing that comes to my mind is that it is more C-ish (read
cryptic) than pythonic (read elegant and understandable).

And "foo if bar" is Perl-ish; yet, even Perl has the ? : operators.

Perl's "foo if bar" is very different to this one, you should know about this.

For a conditional, syntax must be found, and the tradition of Python
design is not to use punctuation for something that can be solved with
keywords.

Reinhold
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top