New assignmens ...

Antoon Pardon antoon.pardon at vub.be
Fri Oct 29 10:03:37 EDT 2021



Op 28/10/2021 om 19:36 schreef Avi Gross via Python-list:
> Now for a dumb question. Many languages allow a form of setting a variable to a value like:
>
>   
>
>                  assign(var, 5+sin(x))
>
>   
>
> If we had a function that then returned var or the value of var, cleanly, then would that allow an end run on the walrus operator?
>
>   
>
> if (assign(sign, 5+sin(x)) <= assign(cosign, 5+cos(x))) …
>
>   
>
> Not necessarily pretty and I am sure there may well be reasons it won’t work, but I wonder if it will work in more places than the currently minimal walrus operator.

This was the orginal code to illustrate the question:

     if (self.ctr:=self.ctr-1)<=0

So if I understand your sugested solution it would be something like:

     def setxattr(obj, attr, value):
         setattr(obj, attr, value)
         return value

     if setxattr(self, 'ctr', self.ctr - 1) <= 0

Did I get that right?

-- 
Antoon Pardon.

    



More information about the Python-list mailing list