[Python-ideas] A partial (wacky?) alternative to assignment expressions

Chris Angelico rosuav at gmail.com
Mon May 14 20:40:35 EDT 2018

On Tue, May 15, 2018 at 10:35 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> We would need to flag which expression can be cached because it is PURE,
> and tag how far the CACHE operates over:
>         <PURE>
>             func(arg)
>         <END PURE>
>         + func(arg)*2 + func(arg)**2
>     <END CACHE>
> This would tell the compiler to only evaluate the sub-expression
> "func(arg)" once, cache the result, and re-use it each other time it
> sees that same sub-expression within the surrounding expression.
> To be clear: it doesn't matter whether or not the sub-expression
> actually is pure. And it doesn't have to be a function call: it could be
> anything legal in an expression.
> If we had this, with appropriately awesome syntax, would that negate the
> usefulness of assignment expresions in your mind?

Not for me, no. It doesn't eliminate lengthy expressions, only costly
ones. It also doesn't deduplicate for convenience of editing. So
that's one goal achieved, two not achieved.

If the function is actually pure, all you need is lru_cache and you
get that result. If it isn't, you've hit one of the two hardest
problems in computing, good luck.


More information about the Python-ideas mailing list