[Python-Dev] Assignment expression and coding style: the while True case
rob.cliffe at btinternet.com
Wed Jul 4 21:35:42 EDT 2018
On 05/07/2018 00:15, Nathaniel Smith wrote:
> On Wed, Jul 4, 2018 at 3:51 PM, Victor Stinner <vstinner at redhat.com> wrote:
>> My question is now: for which "while True" patterns are the assignment
>> expression appropriate? There identified different patterns.
>> == Pattern 1, straighforward ==
>> while True:
>> line = input.readline()
>> if not line:
>> IMHO here assingment expression is appropriate here. The code remains
>> straighfoward to read.
>> while (line := input.readline()):
> There are some obvious caveats here – no-one has real experience with
> := yet, so any opinions right now are informed guesswork that will
> probably change some if/when we get more experience with it.
Of course, this is absolutely true. But ...
> Also, the
> stdlib is a big place, and it seems inevitable that maintainers of
> different modules will have different preferences. So I don't think it
> makes sense to try to define some Official Universal Rule about when
> := is appropriate and when it isn't.
> That said, FWIW, my current feeling is that this simplest case is the
> only one where I would use :=; for your other examples I'd stick with
> the loop-and-a-half style.
... even you, Nathaniel (correct me if I'm wrong, but you seem to be
generally against PEP 572) would use := in (at least) one case.
I predict: We'll all end up loving Assignment Expressions (like
f-strings) once we've got used to them. Or just taking them for
granted. No doubt some of us will use them more than others, some
perhaps not at all.
More information about the Python-Dev