Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Walrus Operator in 3.8
#11
Algol and Algol68 both used the walrus operator for assignment to avoid the ambiguity between assignment and equality testing. Personally I find Python's use of == for equality testing ugly and would prefer a special operator like := (APL uses a left arrow) for all assignments, leaving = for equality testing only. That's not going to happen.

But I don't see why := is needed at all. Take the construct

m = myfunc(args)
if m == 27:
    do something
using walrus notation it becomes

if m := myfunc(args) == 27:
    do something
but why can't Python interpret the following in the same way?

if m = myfunc(args) == 27:
    do something
or to make the intent clearer (but parentheses really not required)

if (m = myfunc(args)) == 27:
    do something
if "myfunc(args)" results in a value which is then assigned to m, then certainly m is an expression that evaluates to the same value, and "m = myfunc(args)" should be allowed in the conditional the same as "myfunc(args)".
Reply
#12
(Jul-19-2019, 01:04 PM)Reverend_Jim Wrote: but why can't Python interpret the following in the same way?

Because assignment is a statement, not an expression, and what goes into the if clause must be an expression. To do what you are suggesting would requiring fundamental changes to the Python syntax.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#13
I like the new syntax.

data = [42, 1337, 13, 15, 7]
a = data[0]
[(a, a:=x) for x in data[1:]]
Output:
[(42, 1337), (1337, 13), (13, 15), (15, 7)]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#14
(Jul-19-2019, 01:04 PM)Reverend_Jim Wrote: but why can't Python interpret the following in the same way?

if m = myfunc(args) == 27:
    do something

That syntax was one of the proposals, and was rejected because it isn't visually different enough from an equality check (==). The whole point is to avoid ambiguity, or to accidentally mistype and cause a re-assignment (which is why you see Yoda statements in other languages [if False == some_value]).
Reply
#15
omg, I just had a duh moment...
Doh
walrus operator named for it looking like one, the walrus emoticon :=
A little slow on that one.
Recommended Tutorials:
Reply
#16
was rejected because it isn't visually different enough from an equality check (==)

I realize this is not going to happen but that statement is justification for using the walrus operator everywhere as in

m := fnord(args)
and deprecating "=" for any assignment. My point was that having two assignment operators is one too many.
Reply
#17
(Jul-20-2019, 04:15 PM)Reverend_Jim Wrote: was rejected because it isn't visually different enough from an equality check (==)

I realize this is not going to happen but that statement is justification for using the walrus operator everywhere as in

m := fnord(args)
and deprecating "=" for any assignment. My point was that having two assignment operators is one too many.



i get a syntax error in 3.8 if i try to do that
>>> m := 10
  File "<stdin>", line 1
    m := 10
      ^
SyntaxError: invalid syntax
Recommended Tutorials:
Reply
#18
Exceptional cases
Need to be parenthesized.
>>> m := 10:
  File "<stdin>", line 1
    m := 10:
      ^
SyntaxError: invalid syntax
>>> 
>>> (m := 10)
10
Reply
#19
i found myself coming back to this thread (a few times while reading online about the walrus op over this entire year)... and having trouble each time understanding the new operator. However this example really does explain it for me, so i figured i would add it here for the next time i need to understand it again.

print("With Python 3.8 Walrus Operator:") 
for entry in sample_data: 
    if title := entry.get("title"):
        print(f'Found title: "{title}"')

print("Without Walrus operator:")
for entry in sample_data:
    title = entry.get("title")
    if title:
        print(f'Found title: "{title}"')
Recommended Tutorials:
Reply
#20
If m := my_func(args) == 42:
    pass
The above is pointless. Why not:
if my_func(args) == 42:
    pass
Which is a way more readable. Of course if one doesn't need 'm'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020