[Python-ideas] Keyword only argument on function call

Robert Vanden Eynde robertve92 at gmail.com
Fri Sep 7 13:17:30 EDT 2018

If you want to force using pos args, go ahead and use Python docstring
notation we'd write def quad(a,b,c, /)

The names should not be renamed because they already have a normal ordering
x ** n.

This notation is standard, so it would be a shame to use something people
don't use.

However, I recently used a quad function in one of my uni course where the
different factors are computed with a long expression, so keyword
arguments, so I'd call:

Vout = quad(
    a=... Some long expression
        spanning a lot of lines ...,
    b=... Same thing ...,
    c=... Same thing...)

Without the a= reminder, one could count the indentation.

And if you'd think it's a good idea to refactor it like that ...

a = ... Some long expression
        spanning a lot of lines ...
b = ... Same thing ...
c = ... Same thing...

Vout = quad(a,b,c)

Then you're in the case of quad(*, a, b, c) (even if here, one would never
def quad(c,b,a)).

Wheter or not this refactor is more clear is a matter of "do you like
functional programming".

However, kwargs arz more useful in context where some parameters are
optional or less frequentely used. But it makes sense (see Pep about
mandatory kwargs).

Kwargs is a wonderful invention in Python (or, lisp).

Le ven. 7 sept. 2018 à 18:54, David Mertz <mertz at gnosis.cx> a écrit :

> Here's a function found online (I'm too lazy to write my own, but it would
> be mostly the same). Tell me how keyword arguments could help this... Or
> WHAT names you'd give.
>    1. def quad(a,b,c):
>    2. """solves quadratic equations of the form
>    3. aX^2+bX+c, inputs a,b,c,
>    4. works for all roots(real or complex)"""
>    5. root=b**2-4*a*c
>    6. if root <0:
>    7. root=abs(complex(root))
>    8. j=complex(0,1)
>    9. x1=(-b+j+sqrt(root))/2*a
>    10. x2=(-b-j+sqrt(root))/2*a
>    11. return x1,x2
>    12. else:
>    13. x1=(-b+sqrt(root))/2*a
>    14. x2=(-b-sqrt(root))/2*a
>    15. return x1,x2
> After that, explain why forcing all callers to name their local variables
> a, b, c would be a good thing.
> On Fri, Sep 7, 2018, 12:18 PM Robert Vanden Eynde <robertve92 at gmail.com>
> wrote:
>>> I disagree.  Keyword arguments are a fine and good thing, but they are
>>> best used for optional arguments IMHO.  Verbosity for the sake of
>>> verbosity is not a good thing.
>> I disagree, when you have more than one parameter it's sometimes
>> complicated to remember the order. Therefore, when you name your args, you
>> have way less probability of passing the wrong variable, even with only one
>> arg.
>> Verbosity adds redundancy, so that both caller and callee are sure they
>> mean the same thing.
>> That's why Java has types everywhere, such that the "declaration part"
>> and the "use" part agree on the same idea (same type).
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180907/d3ace637/attachment-0001.html>

More information about the Python-ideas mailing list