[Python-ideas] Improving fn(arg=arg, name=name, wibble=wibble) code

Michael Selik mike at selik.org
Fri Sep 14 03:19:01 EDT 2018


On Thu, Sep 13, 2018, 11:58 PM Anders Hovmöller <boxed at killingar.net> wrote:

> In that case, you should be able to link to a compelling example. If you
>> go to the trouble of finding one, I'll take time to try to refactor it.
>>
>>
>> https://github.com/django/django/blob/master/django/db/models/sql/compiler.py#L707
>>
>> Is a pretty typical one.
>>
>
> That call is recursive, so it's unlikely that the author would shift the
> parameters around without testing the call and changing the argument
> positions appropriately.
>
>
> Maybe. Still it would be better with keyword arguments. Testing is one
> thing, quickly finding problems is another. Keyword arguments fail fast and
> cleanly with signature changes, positional arguments only do when you add
> or remove parameters at the very end, all other changes are potentially
> very annoying to debug because you can get a long way past the problem call
> site before hitting a type or behavior error.
>
> I'm not sure we even agree on this basic point though. Do you agree on
> that?
>

I agree those are benefits to keyword arguments, but I disagree that those
benefits accrue in all cases. I do not believe that keyword arguments are
strictly better than positional.

Maybe our difference of opinion stems from tooling and the way others
refactor the code we work on.

I enjoy using keywords to explain the values I'm passing. If I already have
a well-named variable, I'm less keen on using a keyword.


Here's a possible refactor. I didn't bother with keyword arguments, because
the variable names are easy to match up with arguments positionally. My
screen was large enough that I could read the signature at the same time as
I wrote the call.

def recurse(name):
    return self.find_ordering_name(name, opts, alias, order, already_seen)
return itertools.chain.from_iterable(map(recurse, opts.ordering))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180914/432ec2f7/attachment-0001.html>


More information about the Python-ideas mailing list