Jul-19-2019, 01:04 PM
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
But I don't see why := is needed at all. Take the construct
m = myfunc(args) if m == 27: do somethingusing walrus notation it becomes
if m := myfunc(args) == 27: do somethingbut why can't Python interpret the following in the same way?
if m = myfunc(args) == 27: do somethingor to make the intent clearer (but parentheses really not required)
if (m = myfunc(args)) == 27: do somethingif "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)".