Python: 打印的打字签名是什么?
Python: What is the typing signature for print?
当定义为 Callable 参数时,print 或类似函数的类型签名是什么,它采用可变数量的任何类型的参数?
具体来说,这里output_function
的声明是什么?
def f(x: str, output_function=print):
output_function(x)
更新:澄清为可调用参数。
来自PEP 484
Arbitrary argument 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)
所以 print
将是:
from typing import Any, IO
def print(*args: Any, sep: str = ' ', \
end: str = '\n', file: IO = sys.stdout, \
flush: bool = False) -> None:
不过我认为您不能将此应用于 Callable
。来自 docs for typing
,
There is no syntax to indicate optional or keyword arguments; such function types are rarely used as callback types. Callable[..., ReturnType]
(literal ellipsis) can be used to type hint a callable taking any number of arguments and returning ReturnType
当定义为 Callable 参数时,print 或类似函数的类型签名是什么,它采用可变数量的任何类型的参数?
具体来说,这里output_function
的声明是什么?
def f(x: str, output_function=print):
output_function(x)
更新:澄清为可调用参数。
来自PEP 484
Arbitrary argument 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)
所以 print
将是:
from typing import Any, IO
def print(*args: Any, sep: str = ' ', \
end: str = '\n', file: IO = sys.stdout, \
flush: bool = False) -> None:
不过我认为您不能将此应用于 Callable
。来自 docs for typing
,
There is no syntax to indicate optional or keyword arguments; such function types are rarely used as callback types.
Callable[..., ReturnType]
(literal ellipsis) can be used to type hint a callable taking any number of arguments and returningReturnType