定义types.Dict和dict之间的区别? [英] Difference between defining typing.Dict and dict?

查看:587
本文介绍了定义types.Dict和dict之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python 3.5中使用类型提示练习。我的一个同事使用 typing.Dict

 导入键入


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)

如果__name__ ==__main__:
main()

他们都工作得很好,似乎没有什么区别。



我已阅读 打字模块文档



typing.Dict dict 我应该在程序中使用哪一个?

解决方案

没有什么不同之间使用一个简单的 typing.Dict dict ,no。



但是, typing.Dict 是一个 通用类型 ,可让您指定密钥和值的类型 ,使其更灵活:

  def change_bandwidths(new_bandwidths:typing.Dict [str,str],
user_id:int,
user_name:str) - > bool:

因此,很可能是在您的项目生命周期的某个时候你想定义字典参数更精确一点,在这一点上将 typing.Dict 扩展到 typing.Dict [key_type,value_type] 是替换 dict 的更小的变化。



您可以使用 映射 或< a href =https://docs.python.org/3/library/typing.html#typing.MutableMapping =nofollow> MutableMapping 这里的类型由于您的功能不需要更改映射,所以我会坚持使用映射。一个 dict 是一个映射,但是您可以创建也满足映射界面的其他对象,并且您的功能可能仍然可以使用:

  def change_bandwidths(new_bandwidths:typing.Mapping [str,str],
user_id:int,
user_name:str) - > bool:

现在您明确地告诉其他用户此功能,您的代码实际上不会>

您的实际执行只是期待一个对象打印。这可能是一个测试实现,但是如果您使用 new_bandwidths:typing.Any ,则代码将继续工作,因为Python中的任何对象都是可打印的。


I am practicing using type hints in Python 3.5. One of my colleague uses 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()

Both of them work just fine, there doesn't appear to be a difference.

I have read the typing module documentation.

Between typing.Dict or dict which one should I use in the program?

解决方案

There is no real difference between using a plain typing.Dict and dict, no.

However, typing.Dict is a Generic type that lets you specify the type of the keys and values too, making it more flexible:

def change_bandwidths(new_bandwidths: typing.Dict[str, str],
                      user_id: int,
                      user_name: str) -> bool:

As such, it could well be that at some point in your project lifetime you want to define the dictionary argument a little more precisely, at which point expanding typing.Dict to typing.Dict[key_type, value_type] is a 'smaller' change than replacing dict.

You can make this even more generic by using Mapping or MutableMapping types here; since your function doesn't need to alter the mapping, I'd stick with Mapping. A dict is one mapping, but you could create other objects that also satisfy the mapping interface, and your function might well still work with those:

def change_bandwidths(new_bandwidths: typing.Mapping[str, str],
                      user_id: int,
                      user_name: str) -> bool:

Now you are clearly telling other users of this function that your code won't actually alter the new_bandwidths mapping passed in.

Your actual implementation is merely expecting an object that is printable. That may be a test implementation, but as it stands your code would continue to work if you used new_bandwidths: typing.Any, because any object in Python is printable.

这篇关于定义types.Dict和dict之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆