Movatterモバイル変換


[0]ホーム

URL:


[Python-ideas] Positional-only parameters

Victor Stinnervictor.stinner at gmail.com
Tue Feb 28 16:17:31 EST 2017


Hi,For technical reasons, many functions of the Python standard librariesimplemented in C have positional-only parameters. Example:-------$ ./pythonPython 3.7.0a0 (default, Feb 25 2017, 04:30:32)>>> help(str.replace)replace(self, old, new, count=-1, /)   # <== notice "/" at the end    ...>>> "a".replace("x", "y")  # ok'a'>>> "a".replace(old="x", new="y")   # ERR!TypeError: replace() takes at least 2 arguments (0 given)-------When converting the methods of the builtin str type to the internal"Argument Clinic" tool (tool to generate the function signature,function docstring and the code to parse arguments in C), I asked ifwe should add support for keyword arguments in str.replace(). Theanswer was quick: no! It's a deliberate design choice.Quote of Yury Selivanov's message:"""I think Guido explicitly stated that he doesn't like the idea toalways allow keyword arguments for all methods. I.e. `str.find('aaa')`just reads better than `str.find(needle='aaa')`. Essentially, the ideais that for most of the builtins that accept one or two arguments,positional-only parameters are better."""http://bugs.python.org/issue29286#msg285578I just noticed a module on PyPI to implement this behaviour on Python functions:https://pypi.python.org/pypi/positionalMy question is: would it make sense to implement this feature inPython directly? If yes, what should be the syntax? Use "/" marker?Use the @positional() decorator?Do you see concrete cases where it's a deliberate choice to denypassing arguments as keywords?Don't you like writing int(x="123") instead of int("123")? :-) (I knowthat Serhiy Storshake hates the name of the "x" parameter of the intconstructor ;-))By the way, I read that "/" marker is unknown by almost all Pythondevelopers, and [...] syntax should be preferred, butinspect.signature() doesn't support this syntax. Maybe we should fixsignature() and use [...] format instead?Replace "replace(self, old, new, count=-1, /)" with "replace(self,old, new[, count=-1])" (or maybe even not document the defaultvalue?).Python 3.5 help (docstring) uses "S.replace(old, new[, count])".Victor


More information about the Python-ideasmailing list

[8]ページ先頭

©2009-2026 Movatter.jp