[Python-Dev] PEP 572: Assignment Expressions

Antoine Pitrou solipsis at pitrou.net
Mon Apr 23 14:44:28 EDT 2018


On Mon, 23 Apr 2018 16:59:35 +0100
Steve Holden <steve at holdenweb.com> wrote:
> On Mon, Apr 23, 2018 at 8:28 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> 
> > On Mon, 23 Apr 2018 00:44:44 -0500
> > Tim Peters <tim.peters at gmail.com> wrote:
> > ​[...]
> >  
> > > if (diff := x - x_base) and (g := gcd(diff, n)) > 1:
> > >     return g
> > > That's so Pythonic I could cry ;-)  
> >
> > ​[...]
> >
> > The second part, especially, where you use the result of an
> > assignment expression as a comparison operand, looks definitely
> > un-Pythonic.
> >
> ​Which, I suppose, underlines that Pythonicity is in the mind of the  
> beholder.​

Indeed it is.  What we can only say is that the proposed idiom goes
against current Python syntactical rules :-)

> The assignment expression seems like a vary natural way to introduce
> variables of limited (controlled?) scope, [...]

AFAIU, the scope isn't limited to the "if" block, it's a regular local
variable.  I might have misread.

> While Tim's expression might look (superficially) like C, the five-line
> alternative isn't exactly an inspiring example of Pythonicity, is it?

I don't know.  I've written my share of similar-looking code and I've
never really been bothered, at least not enough that I thought we
should change the language to accomodate those use cases.  To be frank
I don't remember the last time I was bothered by an aspect of Python's
syntax.  I think the language has reached "peak syntax" by now and it
should basically be frozen, and any improvements targeted at other
aspects (semantics, performance, stdlib, etc.).

Regardless, my three questions about this are:
- does it make Python more powerful?
- does it make Python more readable?
- does it make Python easier to learn and teach?

My answer would be "no" to all three, but YMMV.

Regards

Antoine.


More information about the Python-Dev mailing list