定义 typing.Dict 和 dict 的区别?
Difference between defining typing.Dict and dict?
我正在练习在 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
module documentation。
在typing.Dict
和dict
之间我应该在程序中使用哪一个?
使用普通 typing.Dict
和 dict
之间没有真正的区别,没有。
但是,typing.Dict
是一个 Generic type *,它允许您指定键和值的类型 [=51] =],使其更灵活:
def change_bandwidths(new_bandwidths: typing.Dict[str, str],
user_id: int,
user_name: str) -> bool:
因此,很可能在您的项目生命周期中的某个时刻,您想更精确地定义字典参数,此时将 typing.Dict
扩展到 typing.Dict[key_type, value_type]
是一个 'smaller' 更改而不是替换 dict
.
您可以在此处使用 Mapping
or MutableMapping
类型使其更加通用;由于您的函数不需要 改变 映射,我会坚持使用 Mapping
。 dict
是一个映射,但您可以创建也满足映射接口的其他对象,并且您的函数可能仍然适用于这些对象:
def change_bandwidths(new_bandwidths: typing.Mapping[str, str],
user_id: int,
user_name: str) -> bool:
现在您清楚地告诉此函数的其他用户您的代码实际上不会改变传入的new_bandwidths
映射。
您的实际实现只是期望一个可打印的对象。这可能是一个测试实现,但就目前而言,如果您使用 new_bandwidths: typing.Any
,您的代码将继续工作,因为 Python 中的任何对象都是可打印的。
*:注意:如果您使用的是 Python 3.7 或更新版本,则可以使用 dict
作为通用类型,前提是您使用from __future__ import annotations
, and as of Python 3.9, dict
(as well as other standard containers) supports being used as generic type even without that directive.
typing.Dict
是 dict
的通用版本:
class typing.Dict(dict, MutableMapping[KT, VT])
A generic version of dict. The usage of this type is as follows:
def get_position_in_index(word_list: Dict[str, int], word: str) -> int:
return word_list[word]
这里可以指定字典中key和value的类型:Dict[str, int]
如python org中所述:
class typing.Dict(dict, MutableMapping[KT, VT])
A generic version of dict. Useful for annotating return types. To
annotate arguments it is preferred to use an abstract collection type
such as Mapping.
该类型可以如下使用:
def count_words(text: str) -> Dict[str, int]:
...
但是 dict
不太通用,您将能够更改传入的映射。
事实上,在 python.Dict
中您指定了更多详细信息。
另一个提示:
Deprecated since version 3.9: builtins.dict now supports []. See PEP 585
and Generic Alias Type.
我正在练习在 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
module documentation。
在typing.Dict
和dict
之间我应该在程序中使用哪一个?
使用普通 typing.Dict
和 dict
之间没有真正的区别,没有。
但是,typing.Dict
是一个 Generic type *,它允许您指定键和值的类型 [=51] =],使其更灵活:
def change_bandwidths(new_bandwidths: typing.Dict[str, str],
user_id: int,
user_name: str) -> bool:
因此,很可能在您的项目生命周期中的某个时刻,您想更精确地定义字典参数,此时将 typing.Dict
扩展到 typing.Dict[key_type, value_type]
是一个 'smaller' 更改而不是替换 dict
.
您可以在此处使用 Mapping
or MutableMapping
类型使其更加通用;由于您的函数不需要 改变 映射,我会坚持使用 Mapping
。 dict
是一个映射,但您可以创建也满足映射接口的其他对象,并且您的函数可能仍然适用于这些对象:
def change_bandwidths(new_bandwidths: typing.Mapping[str, str],
user_id: int,
user_name: str) -> bool:
现在您清楚地告诉此函数的其他用户您的代码实际上不会改变传入的new_bandwidths
映射。
您的实际实现只是期望一个可打印的对象。这可能是一个测试实现,但就目前而言,如果您使用 new_bandwidths: typing.Any
,您的代码将继续工作,因为 Python 中的任何对象都是可打印的。
*:注意:如果您使用的是 Python 3.7 或更新版本,则可以使用 dict
作为通用类型,前提是您使用from __future__ import annotations
, and as of Python 3.9, dict
(as well as other standard containers) supports being used as generic type even without that directive.
typing.Dict
是 dict
的通用版本:
class typing.Dict(dict, MutableMapping[KT, VT])
A generic version of dict. The usage of this type is as follows:
def get_position_in_index(word_list: Dict[str, int], word: str) -> int: return word_list[word]
这里可以指定字典中key和value的类型:Dict[str, int]
如python org中所述:
class typing.Dict(dict, MutableMapping[KT, VT])
A generic version of dict. Useful for annotating return types. To annotate arguments it is preferred to use an abstract collection type such as Mapping.
该类型可以如下使用:
def count_words(text: str) -> Dict[str, int]:
...
但是 dict
不太通用,您将能够更改传入的映射。
事实上,在 python.Dict
中您指定了更多详细信息。
另一个提示:
Deprecated since version 3.9: builtins.dict now supports []. See PEP 585 and Generic Alias Type.