对于类型检查,在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"
这样就可以看到问题所在,只要按照对应的类型修改就好了
网友评论