2/2/2023

The walrus (python 3.8) operator is so annoying

I get that it's not meant to be used for funny golfing reasons but please why won't it work when it seems like it should. I have spent many many hours breaking my head over this.

The PEP says that things like:

a = b := c

are not allowed because uh uh uh readability.

IMO, consistency is way more important than readability because that feels so much more, well, consistent. lol

Like, I can't even do things like:

lol = [(2, 3)] # Assume more elements here
for (x, pair := (a, b)) in enumerate(lol):
    pass

Here is what I propose

Simply make := return the left hand side argument. Nothing else. Literally that simple.

I, in my infinite generosity, provide an implementation below:

def __walrus__(self, other):
    self.value = other
    return self.value

I considered returning the right hand side argument, such that:

import random
x = (y := random.random())

would give x and y different values. Will x be a pointer to the random.random function so that x() would be possible or x gets the function call result was the thing that led me to completely give up and just stick to returning LHS values.

But nothing I can do so I have to live with these annoyances.

(I'll add more examples when they occur to me)