[Python-ideas] Keyword only argument on function call
rhodri at kynesim.co.uk
Fri Sep 7 10:43:41 EDT 2018
On 07/09/18 14:59, Anders Hovmöller 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.
> Hmm.. it seems to me like there are some other caveats to your position
> here. Like "no functions with more than two arguments!" or similar?
> Personally I think readability suffers greatly already at two arguments if
> none of the parameters are named. Sometimes you can sort of fix the
> readability with function names like do_something_with_a_foo_and_bar(foo,
> bar), but that is usually more ugly than just using keyword arguments.
I'd have said three arguments in the general case, more if you've chosen
your function name to make it obvious (*not* by that nasty foo_and_bar
method!), though that's pretty rare. That said, I don't often find I
need more than a few mandatory arguments.
> Functions in real code have > 2 arguments. Often when reading the code the
> only way to know what those arguments are is by reading the names of the
> parameters on the way in, because it's positional arguments. But those
> aren't checked. To me it's similar to bracing for indent: you're telling
> the human one thing and the machine something else and no one is checking
> that those two are in sync.
I'll repeat; surprisingly few of my function have more than three
mandatory (positional) arguments. Expecting to understand functions by
just reading the function call and not the accompanying documentation
(or code) is IMHO hopelessly optimistic, and just having keyword
parameters will not save you from making mistaken assumptions.
> I have seen beginners try:
> def foo(b, a):
> a = 1
> b = 2
> foo(a, b)
> and then be confused because a and b are flipped.
I have seen teachers get their students to do that deliberately, to give
them practical experience that the variable names they use in function
calls are not in any way related to the names used in the function
definition. I've not seen those students make the same mistake twice :-)
I wonder if part of my dislike of your proposal is that you are
deliberately blurring that disconnect?
Rhodri James *-* Kynesim Ltd
More information about the Python-ideas