對(duì)于類(lèi)型檢查歌逢,在python中很多人不注意衫生,其實(shí)代碼也不一定報(bào)錯(cuò)媳溺,看不出來(lái)什么問(wèn)題
先來(lái)看下面一段代碼
print(text, align)
return 3
if __name__ == '__main__':
s = headline(1,"4")
print(s)
這段代碼運(yùn)行完全沒(méi)問(wèn)題
因此也看不出來(lái)類(lèi)型檢查在py中的作用洞渤,這里要借助一個(gè)第三方包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"
這樣就可以看到問(wèn)題所在,只要按照對(duì)應(yīng)的類(lèi)型修改就好了