Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP 572 assignment expressions #94

Open
vandys opened this issue Jul 12, 2018 · 8 comments
Open

PEP 572 assignment expressions #94

vandys opened this issue Jul 12, 2018 · 8 comments

Comments

@vandys
Copy link

vandys commented Jul 12, 2018

We should definitely plan on supporting assignment expressions, PEP 572.

The fight for it was so ugly that I see Guido's stepping back from BDFL! :(

@vandys
Copy link
Author

vandys commented Jul 12, 2018

I'm up for adding this, BTW. Is the style of the project that we wait for Python 3 to have it,
and then crib their code as much as possible? (NM, found their WIP patches. Dang, the
language lawyers have sure made Python complex!)

@vandys
Copy link
Author

vandys commented Jul 13, 2018

Ok, I've read the PEP and a bunch of Guido's comments. It's clear he wanted this to be much more
orthogonal, but compromised because of politics (some dictator, huh?).

The truth is := looks a LOT like +=, /=, and friends--augmented assignments. So I'm going to code
it to be another one of those, then make augmented assignments be usable as rvalues, so
you can do:
if ((a := myfunc()) < 2):
for exactly the same reason you can do:
if ((a += INCR) < LIM):
And with right association you get the expected result for:
a := b := 3
Though I can't see why you'd use it over traditional assignment. I must say, when you
say assignments are not rvals, and then you want:
a = b = 3
You have to then right special case code (which cPython does, assignments have an
actual list of lvals to receive the rval).

@naftaliharris
Copy link
Owner

Is the style of the project that we wait for Python 3 to have it,
and then crib their code as much as possible? (NM, found their WIP patches. Dang, the
language lawyers have sure made Python complex!)

Yup, that's right. Here's an example of how I did this for adding the nonlocal keyword: https://www.naftaliharris.com/blog/nonlocal/

@vandys
Copy link
Author

vandys commented Jul 15, 2018

Is Lib/compiler being ignored when adding Python 3 features? The docs appear to say it's
deprecated as of 2.6 (and gone from 3). I can certainly update it if you prefer.

@naftaliharris
Copy link
Owner

naftaliharris commented Jul 15, 2018 via email

@vandys
Copy link
Author

vandys commented Jul 16, 2018

Ok, will test that tomorrow. vandys/tauthon branch pep572 is fairly complete code (he says
hopefully). I made := an augmented assignment, and it along with all other augmented assignments
are usable as expressions. So you can do "while l := foo.read(): ...", but also
"if (ob.count -= 1) > 0: ...", things like that. "a := b := 123" also work as expected,
not because I wrote extra special cases, but because hey, that's how a right associative
expression operator works. I'll flesh out tests, fix an edge case on slices, and also look
at that Python parser; I added the code, but haven't tested.

@vandys
Copy link
Author

vandys commented Aug 5, 2018

I'll tidy this up and resubmit.

@vandys vandys closed this as completed Aug 5, 2018
@vandys
Copy link
Author

vandys commented Aug 5, 2018

Oops, meant to wave off the PR, not the issue. Doh.

This assertion failure points to a deeper issue, thus the PR withdrawal. Python has painted itself
a bit into a corner. A programmer could easily write:

a = 1
b = 2
c = a +=1, b += 1

And expect c == [2, 3] with a == 2 and b == 3.
But this fights you:

a = [1, 2]
a += 3, 4

Which in Python gives you a == [1, 2, 3, 4].

So precedence is distinct for augmented assignment (though their result can still be used
as a value) and the new assignment expression ":=". Which, to meet PEP572, needs
to give the result:

a = 1
c = (a := 2, 3)

with a == 2 and c == [2, 3].

@vandys vandys reopened this Aug 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants