[Python-ideas] Inline assignments using "given" clauses

Steven D'Aprano steve at pearwood.info
Sun May 13 07:32:55 EDT 2018


On Sat, May 12, 2018 at 08:27:53PM -0400, Juancarlo Añez 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.

That's an excellent point. What's "special_gcd" and how does it differ 
from normal gcd? How am I supposed to grok that just from reading the 
code above?

Do I have to dig into the source of special_gcd to understand it?

What happens if the normal gcd would return zero? Is that going to lead 
to a bug?



> It could also be:
> 
> if special_gcd(x - x_base, n) as g:
>     return g

No it can't, because "as" is not an option.




-- 
Steven


More information about the Python-ideas mailing list