typing

https://www.cnblogs.com/poloyy/p/15170297.html

Optional

Optional[int] 等价于 Union[int, None]
意味着:既可以传指定的类型 int,也可以传 None

Literal

一种类型,可用于向类型检查器指示相应的变量或函数参数具有与提供的文字(或多个文字之一)等效的值。例如:

def validate_simple(data: Any) -> Literal[True]:  # always returns True
    ...

MODE = Literal['r', 'rb', 'w', 'wb']
def open_helper(file: str, mode: MODE) -> str:
    ...

open_helper('/some/path', 'r')  # Passes type check
open_helper('/other/path', 'typo')  # Error in type checker

Literal[...]不能被子类化。在运行时,允许将任意值作为类型参数Literal[...],但类型检查器可能会施加限制。看PEP 586有关文字类型的更多详细信息。

本文作者:bitterteaer

本文链接:https://www.cnblogs.com/bitterteaer/p/17751992.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   bitterteaer  阅读(8)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起