gpt4 book ai didi

Python 类型列表[Dict] 与列表[dict]

转载 作者:行者123 更新时间:2023-12-04 11:54:48 28 4
gpt4 key购买 nike

在添加类型提示 python 函数中哪个是首选?

from typing import List, Dict

def example_1() -> List[Dict]:
pass

def example_2() -> List[dict]:
pass
我知道如果我想在 dict 中指定键和值类型,我需要使用 Dict,但我不想。
两者之间有什么区别吗?如果是这样,哪个是首选?

最佳答案

从 Python 3.9 开始,标准集合可以下标。 typing变体是 now deprecated其结果:

tuple # typing.Tuple

list # typing.List

dict # typing.Dict

set # typing.Set

...

Importing those from typing is deprecated. Due to PEP 563 and the intention to minimize the runtime impact of typing, this deprecation will not generate DeprecationWarnings. Instead, type checkers may warn about such deprecated usage when the target version of the checked program is signalled to be Python 3.9 or newer. It's recommended to allow for those warnings to be silenced on a project-wide basis.

The deprecated functionality will be removed from the typing module in the first Python version released 5 years after the release of Python 3.9.0.

关于Python 类型列表[Dict] 与列表[dict],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68199174/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com