New assignmens ...

Jon Ribbens jon+usenet at unequivocal.eu
Thu Oct 28 05:53:53 EDT 2021


On 2021-10-28, Paul Rubin <no.email at nospam.invalid> wrote:
> Chris Angelico <rosuav at gmail.com> writes:
>> But it all depends on the exact process being done, which is why I've
>> been asking for real examples.
>
> My most frequent use case for walrus is so common that I have sometimes
> implemented a special class for it:
>
>    if g := re.search(pat1, text):
>       hack(g.group(1))
>    elif g := re.search(pat2, text):
>       smack(g.group(2), "foo")
>    ...
>
> It's way messier if you have to separate the assignment and test the old
> way.  That said, I'm still on Python 3.7 so I haven't yet gotten to use
> walrus or the new match statement (or is it expression).
>
> I do feel surprised that you can't use an arbitrary lvalue (to use C
> terminology) on the lhs of a walrus.  That seems downright weird to me.
> But, I haven't studied the PEP so I don't know if there was a particular
> rationale.

Well, that's what I was saying: there's no rationale - the limitation
is not even mentioned, let alone explained.


More information about the Python-list mailing list