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

Anders Hovmöller boxed at killingar.net
Fri Sep 14 03:27:46 EDT 2018

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

Maybe. Or the code we have to refactor that others have written. Or both.

> 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 lies the crux for me. You're describing telling the next reader what the arguments are. But without keyword arguments you aren't telling the computer what the arguments are, not really. The position and the names become disconnected when they should be connected. 

It's very similar to how in C you use {} for the computer but indent for the human, and no one checks that they are the same (not true anymore strictly because I believe clang has an option you can turn off to validate this but it's default off). In python you tell the computer and the human the same thing with the same language. This is robust and clear. I think this situation is the same. 

> 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))

I don't see how that changed anything. That just changes it to a functional style, but otherwise it's identical.

/ Anders

More information about the Python-ideas mailing list