The main problem with using /
in the help output for builtins which use Argument Clinic is that this syntax is not documented. If it would be documented, there would be less questions (not more than about *
and **
). I don’t particularly like the /
syntax, but no better syntax was proposed for last 7 years.
I am in a great favor of PEP 570, no matter what syntax will be used. Currently we need to write complex and cumbersome code. Two examples:
class MutableMapping(Mapping):
def update(*args, **kwds):
if not args:
raise TypeError("descriptor 'update' of 'MutableMapping' object "
"needs an argument")
self, *args = args
if len(args) > 1:
raise TypeError('update expected at most 1 arguments, got %d' %
len(args))
if args:
other = args[0]
...
...
class Formatter:
def format(*args, **kwargs):
if not args:
raise TypeError("descriptor 'format' of 'Formatter' object "
"needs an argument")
self, *args = args # allow the "self" keyword be passed
try:
format_string, *args = args # allow the "format_string" keyword be passed
except ValueError:
raise TypeError("format() missing 1 required positional "
"argument: 'format_string'") from None
return self.vformat(format_string, args, kwargs)
And help()
outputs a useless signature for these functions: (*args, **kwargs)
. It is even confusing, because it does not contain self
as signatures of other methods.
With PEP 570 the above examples can be written as:
class MutableMapping(Mapping):
def update(self, other=(), / , **kwds):
...
class Formatter:
def format(self, format_string, /, *args, **kwargs):
return self.vformat(format_string, args, kwargs)
This feature will be useful not only for experts, but it can reduce the confusion of newbies. First, the will no longer be confused by /
in signatures of some builtins. Second, they will no longer be confused by nonavailability to pass self
as a keyword argument to unbound methods.