Decorators don't need inner wrappers, of course

This commit is contained in:
K. Lange 2021-01-11 07:55:31 +09:00
parent 393c3be0b6
commit faba2e9e3f

View File

@ -844,6 +844,22 @@ wrappedFunction()
# Returned from wrapped function
```
The inner wrapper function is not necessary if all the work of the decorator can be done when the function is defined:
```py
def registerCallback(func):
print("Registering callback function",func)
return func
@registerCallback
def aFunction():
print("Hello, world!")
aFunction()
# → Registering callbacuk function <function aFunction>
# Hello, world!
```
Method wrappers work similarly, though be sure to explicitly provide a name (other than `self`) for the object instance:
```py
@ -894,6 +910,8 @@ superSecretFunction("hunter2")
# Welcome!
```
### Keyword Arguments
Arguments may be passed to a function by specifying their name instead of using their positional location.