walrus with a twist :+= or ...

MRAB python at mrabarnett.plus.com
Wed Oct 27 21:20:33 EDT 2021


On 2021-10-28 02:06, Avi Gross via Python-list wrote:
> I just realized I left out **= so my apologies. Are there other such
> abbreviations and does anyone use them?
> 
You forgot about the bitwise operators: |=  &=  ^=

> -----Original Message-----
> From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On
> Behalf Of Avi Gross via Python-list
> Sent: Wednesday, October 27, 2021 8:57 PM
> To: python-list at python.org
> Subject: walrus with a twist :+= or ...
> 
> 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