英文:
Is "Dict" a keyword in python?
问题
在我正在查看的代码库中,有一行代码:
values = Dict[int, List[QuantityClass]]
我在代码库中进行了grep搜索,但无法找到Dict
的定义,并且我认为它不是Python的关键字。这不是Python的关键字,对吗?它看起来像一种自定义的dictionary
,类似于原生的dict
,但我无法通过grep找到其定义。
英文:
In the codebase I'm looking at at work, there's a line
values = Dict[int, List[QuantityClass]]
I cannot find where Dict
is defined anywhere in the codebase when I did a grep, and I don't think it's a keyword in Python? This isn't a keyword right? It looks like some kind of custom dictionary
, analogous to the native dict
, but I'm not able to find its definition with a grep.
答案1
得分: 1
根据评论,Dict
用于类型提示,来自于 from typing import Dict
(或者在您的情况下 from typing import *
)。
话虽如此,我觉得值得分享一种未来调试的方法,而不是使用 grep
。
如果您再次遇到这个问题并且问道:
这是什么?
您可以使用 help
来理解某个东西是什么:
from typing import *
help(Dict)
当然,这仅在对象在作用域内时才有效,但似乎您主要的问题是弄清楚在代码中看到的某个东西是从哪里来的。
英文:
One of the comments made we want to answer. Per the comments, Dict
is for type hinting and comes from from typing import Dict
(or in your case from typing import *
).
That being said, I feel it is relevant to share a way to help debug this in the future instead of grep
.
If you face this again and ask:
> What is this?
You can use help
to understand what something is:
from typing import *
help(Dict)
This of course only works if the object is in scope, but it seems your main question was figuring out where something came from which you saw in code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论