[Python-ideas] Tweaking closures and lexical scoping to include the function being defined

Nick Coghlan ncoghlan at gmail.com
Mon Sep 26 16:59:38 CEST 2011

On Mon, Sep 26, 2011 at 10:31 AM, Ron Adam <ron3200 at gmail.com> wrote:
> Could this help in cleaning up the lru_cash decorator in functools?
> That seems like it may be a good test case as it has both defaults and
> wrappers in it.

Not really - lru_cache actually *needs* the outer function because it
is a decorator factory. Once you have the outer function as a factory
anyway, then the new shorthand syntax becomes somewhat less
significant. lru_cache also shares the state amongst multiple
functions (there are a couple of query operations that are provided as
attributes on the function object). (It's written in a somewhat opaque
way both to minimise runtime overhead and also to discourage people
from becoming dependent on its internal implementation details)

The only thing you could use it to clear up is the manual
micro-optimisation in the decorating function's default arguments, and
that could already be handled by doing the bindings in the outer

That's one of the reasons this is such a niche feature regardless of
how we spell it - it only works when you don't want to share the
closure values across multiple inner functions.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list