[Python-Dev] PEP 572: Assignment Expressions
On Tue, May 1, 2018 at 3:36 AM, Chris Jerdonek <chris.jerdonek at gmail.com>
> On Thu, Apr 26, 2018 at 10:33 AM, Sven R. Kunze <srkunze at mail.de> wrote:
> > On 25.04.2018 01:19, Steven D'Aprano wrote:
> >> Sorry, gcd(diff, n) is not the "perfect name", and I will tell you that
> >> sometimes g is better. [...]
> > We were talking about the real-world code snippet of Tim (as a
> > of := ) and alternative rewritings of it without resorting to new syntax.
> Apologies if this idea has already been discussed (I might have missed
> the relevant email), but thinking back to Tim's earlier example--
> if (diff := x - x_base) and (g := gcd(diff, n)) > 1:
> return g
> it occurs to me this could be implemented with current syntax using a
> pattern like the following:
> stashed = [None]
> def stash(x):
> stashed = x
> return x
> if stash(x - x_base) and stash(gcd(stashed, n)) > 1:
> return stashed
> There are many variations to this idea, obviously. For example, one
> could allow passing a "name" to stash(), or combine stash / stashed
> into a single, callable object that allows setting and reading from
> its store. I wonder if one of them could be made into a worthwhile
> pattern or API..
?I hope you don't think this recasting, is in any way less confusing to a
beginner than an inline assignment. This is language abuse!
In any case, what advantages would it have over simply declaring "stashed"
as a global inside the function and omitting the confusing subscripting?
-------------- next part --------------
An HTML attachment was scrubbed...