You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index.md
+29-28Lines changed: 29 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ The typical use cases are:
21
21
22
22
It currently supports three ways to define the signature of the created function
23
23
24
-
- from a given reference function, e.g. `foo`
24
+
- from a given reference function, e.g. `foo`.
25
25
- from strings, e.g. `'foo(a, b=1)'`
26
26
- from `Signature` objects, either manually created, or obtained by using the `inspect.signature` (or its backport `funcsigs.signature`) method.
27
27
@@ -69,7 +69,8 @@ func_impl called !
69
69
70
70
You can also:
71
71
72
-
* override the function name, docstring and module name if you pass a non-None `func_name`, `doc` and `modulename` argument
72
+
* remove the name from the signature string (e.g. `'(b, a=0)'`) to directly use the function name of `func_impl`.
73
+
* override the function name, docstring, qualname and module name if you pass a non-None `func_name`, `doc`, `qualname` and `module_name` argument
73
74
* add other attributes on the generated function if you pass additional keyword arguments
74
75
75
76
See `help(create_function)` for details.
@@ -96,7 +97,7 @@ def gen_func(*args, **kwargs):
96
97
return args, kwargs
97
98
```
98
99
99
-
It also has the capability to take `None` as a signature, if you just want to update the metadata (`func_name`, `doc`, `modulename`) without creating any function:
100
+
It also has the capability to take `None` as a signature, if you just want to update the metadata (`func_name`, `doc`, `qualname`, `module_name`) without creating any function:
100
101
101
102
```python
102
103
@with_signature(None, func_name='f')
@@ -164,11 +165,11 @@ In many real-world applications we want to reuse "as is", or slightly modify, an
164
165
165
166
#### Copying a signature
166
167
167
-
The easiest way to copy the signature from another function is to directly pass this function as the first argument in `@with_signature` or `create_function`. That way you do not have to use `inspect`/`funcsigs`.
168
+
If you just want to expose the same signature as a reference function (and not wrap it nor appear like it), the easiest way to copy the signature from another function `f` is to use`signature(f)` from`inspect`/`funcsigs`.
168
169
169
170
#### Signature-preserving function wrappers
170
171
171
-
[`@functools.wraps`](https://docs.python.org/3/library/functools.html#functools.wraps) is a famous decorator to create "signature-preserving" function wrappers. However it does not actually preserve the signature, it just uses a trick (setting the `__wrapped__` attribute) to trigger special dedicated behaviour in `stdlib`'s `help()` and `signature()` methods.
172
+
[`@functools.wraps`](https://docs.python.org/3/library/functools.html#functools.wraps) is a famous decorator to create "signature-preserving" function wrappers. However it does not actually preserve the signature, it just uses a trick (setting the `__wrapped__` attribute) to trigger special dedicated behaviour in `stdlib`'s `help()` and `signature()` methods. See [here](https://stackoverflow.com/questions/308999/what-does-functools-wraps-do/55102697#55102697).
172
173
173
174
This has two major limitations:
174
175
@@ -224,30 +225,29 @@ Finally note that a `create_wrapper` function is also provided for convenience ;
224
225
225
226
#### Editing a signature
226
227
227
-
Below we show how to add a parameter to a function. We first capture its `Signature` using `inspect.signature(f)`, we modify it to add a parameter, and finally we use it in `create_function` to create our final function:
228
+
Below we show how to add a parameter to a function. We first capture its `Signature` using `inspect.signature(f)`, we modify it to add a parameter, and finally we use it in `wraps` to create our final function:
228
229
229
230
```python
230
-
from makefun importwith_signature
231
+
from makefun importwraps
231
232
from inspect import signature, Parameter
232
233
233
234
# (0) the reference function
234
235
deffoo(b, a=0):
235
236
print("foo called: b=%s, a=%s"% (b, a))
236
237
return b, a
237
238
238
-
# (1a) capture the name and signature of reference function `foo`
They might save you a few lines of code if your use-case is not too specific.
@@ -372,11 +373,11 @@ prints the following source code:
372
373
373
374
```python
374
375
deffoo(b, a=0):
375
-
return_call_handler_(b=b, a=a)
376
+
return_func_impl_(b=b, a=a)
376
377
377
378
```
378
379
379
-
The `_call_handler_` symbol represents your implementation. As [already mentioned](#arguments_mapping), you see that the variables are passed to it *as keyword arguments* when possible (`_call_handler_(b=b)`, not simply `_call_handler_(b)`). Of course if it is not possible it adapts:
380
+
The `_func_impl_` symbol represents your implementation. As [already mentioned](#arguments_mapping), you see that the variables are passed to it *as keyword arguments* when possible (`_func_impl_(b=b)`, not simply `_func_impl_(b)`). Of course if it is not possible it adapts:
0 commit comments