typing的主要作用有:
- 类型检查,防止运行时出现参数、返回值类型不符
- 作为开发文档附加说明,方便使用者调用时传入和返回参数类型
- 模块加入不会影响程序的运行不会报正式的错误,pycharm支持typing检查错误时会出现黄色警告
语法:
def 函数名(参数: 数据类型) -> 返回值类型: pass 变量名: 数据类型 = 值
from typing import Sequence # 这个用来存放一堆定义的类型 from typing import NewType # 用来创建一个新的类型,以便后续可以使用这个来规范类型
Any 任意类型
Union[X,Y] 联合类型,X或Y
Option[X] 可选类型,X或None
Tuple[X,Y] 元组类型, 第一个元素X,第二个元素Y,
Callable[[X], Y] 可调用类型,第一个是参数列表,第二个是返回类型
typing包含的类型
AbstractSet = typing.AbstractSet Any = typing.Any AnyStr = ~AnyStr AsyncContextManager = typing.AbstractAsyncContextManager AsyncGenerator = typing.AsyncGenerator AsyncIterable = typing.AsyncIterable AsyncIterator = typing.AsyncIterator Awaitable = typing.Awaitable ByteString = typing.ByteString Callable = typing.Callable ClassVar = typing.ClassVar Collection = typing.Collection Container = typing.Container ContextManager = typing.AbstractContextManager Coroutine = typing.Coroutine Counter = typing.Counter DefaultDict = typing.DefaultDict Deque = typing.Deque Dict = typing.Dict FrozenSet = typing.FrozenSet Generator = typing.Generator Hashable = typing.Hashable ItemsView = typing.ItemsView Iterable = typing.Iterable Iterator = typing.Iterator KeysView = typing.KeysView List = typing.List Mapping = typing.Mapping MappingView = typing.MappingView MutableMapping = typing.MutableMapping MutableSequence = typing.MutableSequence MutableSet = typing.MutableSet NoReturn = typing.NoReturn Optional = typing.Optional Reversible = typing.Reversible Sequence = typing.Sequence Set = typing.Set Sized = typing.Sized TYPE_CHECKING = False Tuple = typing.Tuple Type = typing.Type Union = typing.Union ValuesView = typing.ValuesView
标签:Tuple,python,Callable,Union,typing,类型,Any From: https://www.cnblogs.com/lintest/p/17223503.html