13

I've read PEP 572 about assignment expressions and I found this code to be a clear example where I could use it:

while line := fp.readline():
    do_stuff(line)

But I am confused, from what I read, it is supposed to work just like normal assignment but return the value. But it doesn't appear to work like that:

>>> w:=1
  File "<stdin>", line 1
    w:=1
     ^
SyntaxError: invalid syntax

Now after tinkering with it I realised the following works:

>>> (w:=1)
1

But it feels so unpythonic. It is the only operator that requires parentheses:

>>> w = 1
>>> w + w
2
>>> w == w
True
>>> w is w
True
>>> w < w
False

Is there a reason for it to be treated by the parser differently than literally anything else in Python...? I feel like I am missing something. This is not just an operator.

It would be super useful to use := in the REPL to assign variables as the value would be displayed.


(Update: I do not encourage opinionated discussion on this sensitive topic. Please avoid posting comments or answers other than useful ones.)

Boann
  • 44,932
  • 13
  • 106
  • 138
Benoît P
  • 2,684
  • 9
  • 30
  • 2
    OMG this is so evil... – Philip Tzou Feb 05 '19 at 23:48
  • @PhilipTzou I felt the same when I saw it for the first time, but it is readable and quite useful. – Benoît P Feb 05 '19 at 23:50
  • But now we have two ways for variable assignment (although the := is not recommended in the PEP). – Philip Tzou Feb 05 '19 at 23:52
  • 7
    PEP 572 [actually covers this case](https://www.python.org/dev/peps/pep-0572/#exceptional-cases) - it's to avoid ambiguity. That said, I'm not sure what this question is discussing (feature seems to be working as intended?), and I don't think StackOverflow is the correct place for that discussion. – Green Cloak Guy Feb 05 '19 at 23:54
  • 3
    The `while line := fp.readline():` is also non-sense. It can be simply replaced by a more pythonic way `for line in fp.readline():` or even `for line in fp:`. – Philip Tzou Feb 05 '19 at 23:54
  • It's not in a release yet, is it? – hpaulj Feb 06 '19 at 03:33
  • I thought about why they didn't decide to make assignment an expression, like e.g. C does it, [and I have found their arguments against it pretty weak](https://www.python.org/dev/peps/pep-0572/#why-not-just-turn-existing-assignment-into-an-expression). It's probably more of a political decision, maintaining an "easy to learn and use language". But then you end up with a lot of special cases for everything. PHP should be a warning to that, when they realized they needed a "real" comparison operator, and now it has `==` and `===`. – Jan Christoph Terasa Feb 06 '19 at 07:46
  • @hpaulj It is in [pre-release](https://www.python.org/download/pre-releases/) – Benoît P Feb 06 '19 at 09:17
  • 1
    Interestingly, some of the supposedly invalid usage examples [seem to work for me](https://gist.github.com/myrmica-habilis/5f89c56dc5d97a6269c8edea682f9c5a) in the current alpha version. – mportes Feb 06 '19 at 14:16

1 Answers1

5

As GreenCloakGuy mentioned, it is there to avoid confusion, as said here, I think this line sums it all:

there is no syntactic position where both = and := are valid.

It also makes things like these invalid because too confusing:

y0 = y1 := f(x)
foo(x = y := f(x))
Ayxan Haqverdili
  • 17,764
  • 5
  • 27
  • 57
Benoît P
  • 2,684
  • 9
  • 30