Give an explicit example for how to annotate *args, **kwds.

This commit is contained in:
Guido van Rossum 2016-03-22 14:54:38 -07:00
parent cf6962bc20
commit 6ce45f1256
1 changed files with 18 additions and 2 deletions

View File

@ -885,8 +885,24 @@ Don't expect a checker to understand obfuscations like
``"".join(reversed(sys.platform)) == "xunil"``.
Default argument values
-----------------------
Arbitrary argument lists and default argument values
----------------------------------------------------
Arbitrary agrument lists can as well be type annotated,
so that the definition::
def foo(*args: str, **kwds: int): ...
is acceptable and it means that, e.g., all of the following
represent function calls with valid types of arguments::
foo('a', 'b', 'c')
foo(x=1, y=2)
foo('', z=0)
In the body of function ``foo``, the type of variable ``args`` is
deduced as ``Tuple[str, ...]`` and the type of variable ``kwds``
is ``Dict[str, int]``.
In stubs it may be useful to declare an argument as having a default
without specifying the actual default value. For example::