[Python-ideas] Positional-only parameters

Victor Stinner victor.stinner at gmail.com
Thu Mar 2 10:47:00 EST 2017


2017-03-02 14:23 GMT+01:00 Steven D'Aprano <steve at pearwood.info>:
>> Replace "replace(self, old, new, count=-1, /)" with "replace(self,
>> old, new[, count=-1])" (or maybe even not document the default
>> value?).
>
> That isn't right. It would have to be:
>
> replace([self, old, new, count=-1])
>
> if all of the arguments are positional-only. But that makes it look like
> they are all optional! A very strong -1 to this.
>
>> Python 3.5 help (docstring) uses "S.replace(old, new[, count])".
>
> Should be:
>
> S.replace(old, new[, count], /)
>
> which shows that all three arguments are positional only, but only count
> is optional.

Oh, I didn't notice the weird count parameter: positional-only, but no
default value?

I would prefer to avoid weird parameters and use a syntax which can be
written in Python, like:

   def replace(self, old, new, /, count=-1): ...

When a function has more than 3 parameters, I like the ability to pass
arguments by keyword for readability:

   "xxx".replace("x", "y", count=2)

It's more explicit than:

   "xxx".replace("x", "y", 2)

By the way, I proposed once to convert open() parameters after
filename and mode to keyword-only arguments, but Guido didn't want to
break the backward compatibility ;-)

   open(filename, mode, *, buffering=-1, ...)

Victor


More information about the Python-ideas mailing list