
On 17/04/2020 11:37, Alex Hall wrote:
On Fri, Apr 17, 2020 at 11:57 AM Steven D'Aprano <steve@pearwood.info> wrote:
On Fri, Apr 17, 2020 at 11:25:15AM +0200, Alex Hall wrote:
You look at the function call and you can see a bunch of names being passed to self.do_something. If the function call has 'keyword=keyword' in it instead of just 'keyword', that's not adding much information.
Of course it adds important information! It is adding the critical information: which parameter gets assigned the specified value.
Of course it's adding some information, but it's not information a reader usually has to think about.
Uh, it's information that as a reader I consider critical.
Readers don't generally have to compare two similar looking pieces of code. If they do, it's generally in a conversation with someone (e.g. when reviewing a PR) and they can ask that person what the asterisk means. So "What's the difference between these two calls?" is not usually a relevant question.
Actually if the difference is only a couple of characters, it is relevant. In another thread, it was pointed out that having both get_default() and get_defaults() methods on a class was asking for comedy moments (well, comedy from everyone else's point of view). Similarly, if you look at "f(*, x)", don't understand the star or otherwise slide over it and consequently make mistaken assumptions about what x is, well, it matters quite a lot.
The technical details of how an argument is being passed are usually not important to readers.
I don't care about the argument passing implementation. I care about the meaning of the code. Here is a real-world case:
open('example.txt', encoding, newline, errors, mode)
open('example.txt', *, encoding, newline, errors, mode)
Would you agree that the two of those have *radically* different meanings? The first will, if you are lucky, fail immediately; the second may succeed. But to the poor unfortunate reader who doesn't know what the star does, the difference is puzzling.
This holds even more strongly if the various parameters take the same type:
# a real signature from one of my functions def function( meta:bool, dunder:bool, private:bool, ignorecase:bool, invert:bool)
function(dunder, invert, private, meta, ignorecase) function(*, dunder, invert, private, meta, ignorecase)
No reader will ever have to think about the difference. They will simply see the second version and know which arguments are being passed.
I seem to be immune to this magical knowledge.
Also, your examples are clearly demonstrating that using the shortcut makes it easier to avoid mistakes.
It shows that using positional parameters makes it easier to make mistakes, but that's not news. -- Rhodri James *-* Kynesim Ltd