walrus with a twist :+= or ...

Avi Gross avigross at verizon.net
Wed Oct 27 20:56:59 EDT 2021


I realized that the person seeking completeness in Python may next ask why
the Walrus operator, :=, is not properly extended to include a whole
assortment of allowed assignment operators

 

I mean in normal python programs you are allowed to abbreviate

 

x = x + 5

 

with

 

x += 5

 

Similarly you have other operators like

 

x *= 2

 

And, of course, the constantly used operator:

 

x %= 2

 

So how does one extend a walrus operator if they ever decide to give in and
add it to the language just for completeness?

 

Sadly, a simple test shows they neglected to use a :+= operator in the
latest:

 

>>> (walrus := 2)

2

>>> walrus

2

>>> (wallrus :+= 2)

  File "<stdin>", line 1

    (wallrus :+= 2)

             ^

SyntaxError: invalid syntax

 

(Yes, I know how to spell walrus, but making a point.)

 

On a serious note, if it was ever considered a good idea, what would be an
acceptable sequence of symbols that might not break or confuse existing
programs and what would we call it? I mean what animal, of course.

 

What do these look like in some fonts? :+=   :-=   :*=   :/=   :%=  

 

Or do we not just add a colon in front and make it a tad different as in :=+
or :+=:   or maybe realize the futility of perfection! After all, you can
easily use some functions to get a result such as:

 

x := func(x, "+", 5)

x := func_add(x, 5)

 

or many other work-arounds.

 

Can we all forget I asked? I am sort of being sarcastic.

 



More information about the Python-list mailing list