[LyX/master] Fixup f96b99dc: thinko
José Abílio Matos
jamatos at lyx.org
Fri Oct 2 15:20:04 UTC 2020
On Friday, October 2, 2020 2:46:30 PM WEST Richard Kimberly Heck wrote:
> This is actually fine. Assignment returns a reference to the thing on
> the left, at least by default. That is why
>
> a = b =c
>
> does what you expect.
>
> See
> https://stackoverflow.com/questions/15292892/what-is-return-type-of-assignme
> nt-operator
>
> Still, I find that kind of statement a bit confusing.
>
> Riki
That is the same issue with the more usual type of error:
while (a = b) {
// do something
}
I have my share of bytes due to that. :-)
The condition above only fails when b == 0. :-)
That was one of the reasons why python does not allows assignments in
conditions.
Although sometimes it is very handy to do that. Starting from python 3.8,
python accepts assignment expressions also know as the "walrus operator". :-)
https://www.python.org/dev/peps/pep-0572/
In python the equivalent form to the above while would be:
while a := b
# do something
In our python scripts we have lots of cases where this would very hand, since
it better expresses the intention. An example that we use a lot is:
match1 = pattern1.match(data)
match2 = pattern2.match(data)
if match1:
result = match1.group(1)
elif match2:
result = match2.group(2)
else:
result = None
That snippet becomes with the walrus operator:
if match1 := pattern1.match(data)
result = match1.group(1)
elif match2 := pattern2.match(data):
result = match2.group(2)
else:
result = None
--
José Abílio
More information about the lyx-devel
mailing list