[Tutor] Question about python decorators
Mats Wichmann
mats at wichmann.us
Mon Jul 11 13:23:13 EDT 2022
On 7/11/22 04:58, nathan tech wrote:
> As I understand it, decorators are usuallyed created as:
> @object.event
> Def function_to_be_executed():
> Do_stuff
>
> My question is, is there a way to create this after the function is created?
> So:
> Def function():
> Print("this is interesting stuff")
>
> @myobject.event=function
You don't have to use the @ form at all, but your attempt to assign
won't work.
def function():
print("This is interesting stuff")
function = object.event(function)
Now the new version of function is the wrapped version; the original
version (from your def statement) is held by a reference in the instance
of the wrapper.
If that's what you are asking...
More information about the Tutor
mailing list