walrus operator and expression
![](https://secure.gravatar.com/avatar/de311342220232e618cb27c9936ab9bf.jpg?s=120&d=mm&r=g)
In the following bit of code: while s := input.read(MAXBINSIZE): while len(s) < MAXBINSIZE and ns := input.read(MAXBINSIZE-len(s)): s += ns line = binascii.b2a_base64(s) output.write(line) I'm getting this error on the second line: cannot use assignment expressions with expression Can somebody explain why that isn't working? Many thanks. -- ~Ethan~
![](https://secure.gravatar.com/avatar/047f2332cde3730f1ed661eebb0c5686.jpg?s=120&d=mm&r=g)
Parenthesize. On Mon, Mar 28, 2022 at 3:50 PM Ethan Furman <ethan@stoneleaf.us> wrote:
In the following bit of code:
while s := input.read(MAXBINSIZE): while len(s) < MAXBINSIZE and ns := input.read(MAXBINSIZE-len(s)): s += ns line = binascii.b2a_base64(s) output.write(line)
I'm getting this error on the second line:
cannot use assignment expressions with expression
Can somebody explain why that isn't working?
Many thanks.
-- ~Ethan~ _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/3NONJDUT... Code of Conduct: http://python.org/psf/codeofconduct/
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>
![](https://secure.gravatar.com/avatar/d67ab5d94c2fed8ab6b727b62dc1b213.jpg?s=120&d=mm&r=g)
On Tue, 29 Mar 2022 at 09:53, Ethan Furman <ethan@stoneleaf.us> wrote:
In the following bit of code:
while s := input.read(MAXBINSIZE): while len(s) < MAXBINSIZE and ns := input.read(MAXBINSIZE-len(s)): s += ns line = binascii.b2a_base64(s) output.write(line)
I'm getting this error on the second line:
cannot use assignment expressions with expression
Can somebody explain why that isn't working?
I'm getting a good hint from the exception in 3.11:
while len(s) < MAXBINSIZE and ns := input.read(MAXBINSIZE-len(s)): File "<stdin>", line 1 while len(s) < MAXBINSIZE and ns := input.read(MAXBINSIZE-len(s)): ^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: cannot use assignment expressions with expression
Looks like it's counting "len(s) < MAXBINSIZE and ns" as the assignment target. Parens around the second half would solve that. ChrisA
![](https://secure.gravatar.com/avatar/5ce43469c0402a7db8d0cf86fa49da5a.jpg?s=120&d=mm&r=g)
On 2022-03-28 23:59, Chris Angelico wrote:
On Tue, 29 Mar 2022 at 09:53, Ethan Furman <ethan@stoneleaf.us> wrote:
In the following bit of code:
while s := input.read(MAXBINSIZE): while len(s) < MAXBINSIZE and ns := input.read(MAXBINSIZE-len(s)): s += ns line = binascii.b2a_base64(s) output.write(line)
I'm getting this error on the second line:
cannot use assignment expressions with expression
Can somebody explain why that isn't working?
I'm getting a good hint from the exception in 3.11:
while len(s) < MAXBINSIZE and ns := input.read(MAXBINSIZE-len(s)): File "<stdin>", line 1 while len(s) < MAXBINSIZE and ns := input.read(MAXBINSIZE-len(s)): ^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: cannot use assignment expressions with expression
Looks like it's counting "len(s) < MAXBINSIZE and ns" as the assignment target. Parens around the second half would solve that.
I think that's because assignment has low precedence, lower than that of the RHS, because the RHS needs to be evaluated before the assignment, but the consequence of that it that the preceding expression will also get evaluated first, consuming the identifier, leaving a trailing ':='. It's easier just to complain and require on parentheses than to back up while parsing.
participants (4)
-
Chris Angelico
-
Ethan Furman
-
Guido van Rossum
-
MRAB