[Tutor] Question about python decorators
Roel Schroeven
roel at roelschroeven.net
Mon Jul 11 07:08:42 EDT 2022
Op 11/07/2022 om 12:58 schreef nathan tech:
> 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 can write it as
function = myobject.event
Actually the syntax using @object.event in front of the function is
syntactic sugar for that notation.
Example:
import time
import functools
def slow():
time.sleep(1)
slow = functools.cache(slow)
Now slow() will only be slow on the first call, because subsequent calls
will be served from the cache.
--
"Peace cannot be kept by force. It can only be achieved through understanding."
-- Albert Einstein
More information about the Tutor
mailing list