On Fri, Apr 17, 2020 at 11:25:15AM +0200, Alex Hall wrote:
> I don't think you really need to know what it means to read the code for
> most purposes.
*blink*
> 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.
Put yourself in the position of someone who doesn't know the meaning of
the * in a function call. What's the difference between these two calls?
f(x)
f(*, x)
It's not good enough to merely say that you're passing an argument `x`.
That's true of both calls. There must be a difference between the two,
otherwise why have the star?
Positional arguments tell us that values are assigned to parameters from
left to right:
function(spam, # first parameter
eggs, # second parameter
cheese, # third parameter
)
but we have no clue at all what the names of those parameters are.
That's the down-side of positional arguments, and one of the reasons why
positional arguments don't scale. Knowing the names of the parameters is
often important.
Keyword arguments fill in the missing critical information:
function(alpha=spam,
beta=eggs,
gamma=cheese,
)
and we can shuffle the order around and the mapping of argument value to
parameter name is still clear.
> 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)
[...]
> > Have I missed something?
> >
>
> Something weird seems to have happened in this thread. Rodrigo first
> proposed the magic asterisk syntax here:
> https://mail.python.org/archives/list/[email protected]/message/N2ZY5NQ5T2OJRSUGZJOANEQOGEQIYYIK/
Ah, that's the bit I missed.
--
Steven
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/B7EPSWKYOTBW5MMPBZGUS4NAVAZSQLBD/
Code of Conduct: http://python.org/psf/codeofconduct/