On Fri, Apr 26, 2019 at 8:42 AM Peter O'Connor
<peter.ed.ocon...@gmail.com> wrote:
>
> Dear all,
>
> Despite the general beauty of Python, I find myself constantly violating the 
> "don't repeat yourself" maxim when trying to write clear, fully documented 
> code.  Take the following example:
>
>     def func_1(a: int = 1, b: float = 2.5) -> float:
>         """
>         Something about func_1
>         :param a: Something about param a
>         :param b: Something else about param b
>         :return: Something about return value of func_1
>         """
>         return a*b
>
>     def func_2(c:float=3.4, d: bool =True) -> float:
>         """
>         Something about func_2
>         :param c: Something about param c
>         :param d: Something else about param d
>         :return: Something about return value
>         """
>         return c if d else -c
>
>     def main_function(a: int = 1, b: float = 2.5, d: bool = True) -> float:
>         """
>         Something about main_function
>         :param a: Something about param a
>         :param b: Something else about param b
>         :param d: Something else about param d
>         :return: Something about return value
>         """
>         return func_2(func_1(a=a, b=b), d=d)
>
> Which has the following problems:
> - Defaults are defined in multiple places, which very easily leads to bugs 
> (I'm aware of **kwargs but it obfuscates function interfaces and usually does 
> more harm than good)
>

I'd actually rather explore fixing this problem than the other. We
have functools.wraps() for the case where you do nothing other than
pass through *a,**kw, but when you want to add or remove an argument,
I don't think there's an easy way to say "that function's signature,
but with these changes". That way, you aren't obfuscating the
interface (since the called function's signature is incorporated into
the wrapper's), and you're not duplicating defaults or anything.

It shouldn't need to be all that complicated to use (although I'm sure
it'll be complicated to implement). Something like:

@functools.passes_args(f)
def wrapper(spam, ham, *a, **kw):
    f(*a, **kw)

There would need to be parameters to indicate the addition of
parameters, but it could detect the removal (which is common for
wrappers) just from the function's own signature.

If that were implemented, would it remove the need for this new syntax
you propose?

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to