New assignmens ...

Avi Gross avigross at verizon.net
Fri Oct 29 19:43:47 EDT 2021


Antoon,

As long as we understand that my suggestion is not meant to be taken seriously, your extension is along the lines I intended.

You might indeed have a family of helper functions whose purpose is to bot make a change on the side and return the value to be used in a computation. Your specific implementation of something like that:

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

Would need to have access to the original object and change it in a way that propagates properly. So when you do this:

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

Then assuming passing it 'ctr' as a string makes sense, and the object self is passed by reference, I can see it working without a walrus operator.

But it is extra overhead. This being python, setting values WITHIN an object is a challenge. I mean there are ways to make a value readable but not writeable or writeable only using a designated method, or an attempt to set the value may be intercepted and the interceptor may choose to do something different such as ignoring the request if someone tries to set the time to thirteen o'clock or even setting it to 1 o'clock instead. The above kind of code perhaps should not return value but obj.attr so we see what was stored. But again, Python lets you intercept things in interesting ways so I can imagine it showing something other that what you stored. 

Sigh

As noted, the general case implemented walrus style may have challenges and even efforts like the above may not always be straightforward.

Language design is not as trivial as some think and like with many things, adding a neat new feature may open up holes including security holes if people figure out how to abuse it. Shutting down some such abilities is exactly why people code defensively and try to hide the inner aspects of an object by doing things like having a proxy in front of it and creating getters and setters.


-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On Behalf Of Antoon Pardon
Sent: Friday, October 29, 2021 10:04 AM
To: python-list at python.org
Subject: Re: New assignmens ...



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.

    

-- 
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list