使用python3的typing模块提高代码健壮性
发布时间:2020-12-20 10:14:34 所属栏目:Python 来源:网络整理
导读:typing模块的作用: 类型检查,防止运行时出现参数和返回值类型不符合。 作为开发文档附加说明,方便使用者调用时传入和返回参数类型。 该模块加入后并不会影响程序的运行,不会报正式的错误,只有提醒。 注意: typing模块只有在 python3 .5以上的版本中才
注意:typing模块只有在python3.5以上的版本中才可以使用,pycharm目前支持typing检查
from typing import List,Tuple,Dict def add(a:int,string:str,f:float,b:bool) -> Tuple[List,Dict,bool]: list1 = list(range(a)) tup = (string,string,string) d = {"a":f} bl = b return list1,tup,d,bl print(add(5,"hhhh",2.3,False)) # 结果:([0,1,2,3,4],(‘hhhh‘,‘hhhh‘,‘hhhh‘),{‘a‘: 2.3},False)
from typing import List def func(a:int,string:str) -> List[int or str]: list1 = [] list1.append(a) list1.append(string) return list1 # 使用or关键字表示多种类型
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 使用坐标点生成shp图斑
- python用10行代码实现对黄色图片的检测功能
- pycharm导入python包
- 如何将Python 2 unicode()函数转换为正确的Python 3.x语法
- TypeError:write()参数必须是str,而不是字节(Python 3 vs
- 在python中,selenium模块.如何使用firefox驱动程序
- django 1.9:ProgrammingError:关系“users_user”不存在
- python – 关于滑动窗口和memoization的计算
- python – 从.pdf中提取特定数据并保存在Excel文件中
- python – Django的call_command失败,缺少必需的参数