我正在练习在Python 3.5中使用类型提示。我的其中一位同事使用typing.Dict
:
import typing
def change_bandwidths(new_bandwidths: typing.Dict,
user_id: int,
user_name: str) -> bool:
print(new_bandwidths, user_id, user_name)
return False
def my_change_bandwidths(new_bandwidths: dict,
user_id: int,
user_name: str) ->bool:
print(new_bandwidths, user_id, user_name)
return True
def main():
my_id, my_name = 23, "Tiras"
simple_dict = {"Hello": "Moon"}
change_bandwidths(simple_dict, my_id, my_name)
new_dict = {"new": "energy source"}
my_change_bandwidths(new_dict, my_id, my_name)
if __name__ == "__main__":
main()
两者都可以正常工作,似乎没有区别。
我已经阅读了typing
模块文档。
typing.Dict
和dict
之间,在程序中应该使用哪一个?
def a(b: int) -> bool:
在Python 2.7中是语法错误,我认为在早期版本的Python 3中也是语法错误。 - cat