美文网首页
python之类型检查的简单应用

python之类型检查的简单应用

作者: ochipe | 来源:发表于2019-07-11 09:23 被阅读0次

对于类型检查,在python中很多人不注意,其实代码也不一定报错,看不出来什么问题
先来看下面一段代码

 print(text, align)
 return 3


if __name__ == '__main__':
 s = headline(1,"4")
 print(s)

这段代码运行完全没问题

因此也看不出来类型检查在py中的作用,这里要借助一个第三方包mypy

>mypy study_test\tpy.py
study_test\tpy.py:11: error: Incompatible return value type (got "int", expected "str")
study_test\tpy.py:15: error: Argument 1 to "headline" has incompatible type "int"; expected "str"
study_test\tpy.py:15: error: Argument 2 to "headline" has incompatible type "str"; expected "bool"

这样就可以看到问题所在,只要按照对应的类型修改就好了

相关文章

网友评论

      本文标题:python之类型检查的简单应用

      本文链接:https://www.haomeiwen.com/subject/gualkctx.html