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


_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/PG4UIVT3XEWUS5TVO4MJIQV47UOVV3TD/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to