[Python-ideas] Inline assignments using "given" clauses
Chris Angelico
rosuav at gmail.com
Sat May 12 20:41:21 EDT 2018
On Sun, May 13, 2018 at 10:27 AM, Juancarlo Añez <apalala at gmail.com> wrote:
>
>> if (diff := x - x_base) and (g := gcd(diff, n)) > 1:
>> return g
>>
>
> I don't see the advantage in that succinctness:
>
> g = special_gcd(x - x_base, n)
>
> if g:
>
> return g
>
>
> The code bases I work on constantly move towards having the next guy grok
> what's going on just by reading the code.
>
> It could also be:
>
> if special_gcd(x - x_base, n) as g:
>
> return g
>
Now I have to go read elsewhere to figure out what "special_gcd" does.
With Tim's original code, I could see the effect right there. It might
not seem significant with a single function, but if you had this
situation come up a dozen times, now you have a dozen functions, each
one used only once. You have to go a LONG way out of line to find the
meaning of this name.
Remember: Giving a function a useful name means figuring out a name
that means you do not need to read the function's body to understand
what it does. It's easy to say "just make it a function", but that's
utterly useless if the next reader has to grok the function's
implementation to understand its usage.
ChrisA
More information about the Python-ideas
mailing list