@functools.passes_args(f)
def wrapper(spam, ham, *a, **kw):
f(*a, **kw)
....
If that were implemented, would it remove the need for this new syntax
you propose?
This does indeed allow defaults and types to be passed on, but I find a this approach still has the basic flaw of using **kwargs:
- It only really applies to "wrappers" - functions that wrap another function. The goal here is to address the common case of a function passing args to a number of functions within it.
- It is assumed that the wrapper should use the same argument names as the wrapped function. A name should bind a function to an argument - but here the name is packaged with the argument.
- It remains difficult to determine the arguments of "wrapper" by simple inspection - added syntax for removal of certain arguments only complicates the task and seems fragile (lest the wrapped functions argument names change).
- Renaming an argument to "f" will change the change the arguments of wrapper - but in a way that's not easy to inspect (so for instance if you have a call "y = wrapper(arg_1=4)", and you change "f(arg1=....)" to "f(arg_one=...)" no IDE will catch that and make the appropriate change to "y=wrapper(arg_one=4)".
- What happens if you're not simply wrapping one sub-function but calling several? What about when those subfunctions have arguments with the same name?
----------
Why not just this in existing Python:
def func_1(
a: int = 1 # 'Something about param a',
b: float = 2.5 # 'Something else about param b',
) -> float:
"""Something about func_1
a and b interact in this interesting way.
a should be in range 0 < a < 125
floor(b) should be a prime number
Something about return value of func_1
returns a multiplication
"""
- This would currently be a syntax error (because "#" is before the comma), but sure we could put it after the comma.
- It does not address the fact that we cannot reference "func_1.a.default" - which is one of the main points of this proposal.
- I'm fine with "#" being the documentation operator instead of "?", but figured people would not like it because it breaks the precedent of anything after "#" being ignored by the compiler
---------------
Maybe something like...
def foo(**kwargs):
“””
@signature_by: full.module.path.to.a.signature_function(pass_kwargs_to=bar, hardcoded=[‘quux’])
“””
return bar(**kwargs, quux=3)
This makes it difficult to see what the names of arguments to "foo" are, at a glance. And what happens if (as in the example) "foo" does not simply wrap a function, but distributes arguments to multiple subfunctions? (this is a common case)
----------------------------