
On 8/11/2021 7:56 AM, Larry Hastings wrote:
So, here's an idea, credit goes to Eric V. Smith. What if we tweak how decorators work, /juuuust sliiiightly/, so that they work like the workaround code above?
Specifically: currently, decorators are called just after the function or class object is created, before it's bound to a variable. But we could change it so that we first bind the variable to the initial value, then call the decorator, then rebind. That is, this code:
@dekor8 class C: ...
would become equivalent to this code:
class C: ... C = dekorate(C)
This is how function decorators were originally defined. Before the 2016 (3.5) revision of https://docs.python.org/3/reference/compound_stmts.html#function-definitions by https://bugs.python.org/issue26576 --- @f1(arg) @f2 def func(): pass is equivalent to def func(): pass func = f1(arg)(f2(func)) --- After --- @f1(arg) @f2 def func(): pass is roughly equivalent to def func(): pass func = f1(arg)(f2(func)) except that the original function is not temporarily bound to the name func. --- I questioned on the issue whether the non-binding optimization "should it be documented as a guaranteed language feature or as just an optional optimization?" -- Terry Jan Reedy