首页 > 编程语言 >python中的typing库

python中的typing库

时间:2023-03-16 17:34:49浏览次数:42  
标签:Tuple python Callable Union typing 类型 Any

typing的主要作用有:

  1. 类型检查,防止运行时出现参数、返回值类型不符
  2. 作为开发文档附加说明,方便使用者调用时传入和返回参数类型
  3. 模块加入不会影响程序的运行不会报正式的错误,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

相关文章

  • 20230314-python-字典与json
    1.字典的定义                      ......
  • 小白也能学会的精简版GA遗传算法(Python)
    今天无意中看到了一篇讲遗传算法的文章,文章内容很短,大部分都是代码,代码跟平时见到的遗传算法不同之所以要拿这篇文章来讲,主要是因为原文没有对代码进行解释,但是,这段......
  • python datetime模块常用功能
    时间的转换:时间戳转日期(datetime.date.fromtimestamp(1234567896)),返回日期年-月-日时间戳转年月日时分秒(datetime.datetime.fromtimestamp(123456789......
  • python 雪花算法demo
    importtimeimportloggingclassInvalidSystemClock(Exception):"""时钟回拨异常"""pass#64位ID的划分WORKER_ID_BITS=5DATACENTER_ID_B......
  • python工程打包成可执行文件
    1、将python打包成exe的方式python上常见的打包方式目是通过pyinstaller来实现的。pipinstallpyinstaller或者用镜像下载:#清华源pipinstallpyinstaller-i......
  • python选出一定数量的随机文件到某个文件夹
    importosimportrandomimportshutildefmove_file(target_path,save_path,number):file_list=os.listdir(target_path)random.shuffle(file_list)......
  • python用递归方式去掉首尾空格
    #递归去除空格deftrip_str(s):   ifs[0]=='':       returntrip_str(s[1:])   elifs[-1]=="":       returntrip_str(s[:-1])......
  • Python中列表去重常用的3种方法!
    在Python中,列表去重的方法有很多种,其中比较常用的方法有3种:1、利用字典的【fromkeys()】和【keys()】方法去重;2、集合的可迭代方法;3、用for循环。这3种方法希望大家可......
  • Python3实现的简单三级菜单功能示例(Python实现三级菜单)
    三级菜单_要求:1.运行程序输出第一级菜单2.选择一级菜单某项,输出二级菜单,同理输出三级菜单3.菜单数据保存在文件中4.让用户选择是否要退出5.有返回上一级菜单的功能......
  • python中的os库
    os是一个标准库,专门用来操作系统接口,安装位置可以通过os.__file__属性得到。windows操作系统的分隔符是:反斜杠\Linux的分隔符是:正斜杠/  1.文件和目录os.getcw......