是否可以“提示"?字典键? [英] Is it possible to "hint" dictionary keys?

查看:45
本文介绍了是否可以“提示"?字典键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PyCharm 代码补全 >基本补全"> 调用基本补全" > 字典" 我看到,如果你将字典硬编码为某些值,你可以在编写关于该字典的代码时使用代码补全.

In PyCharm Code completion > "Basic Completion" > "Invoke Basic Completion" > "Dictionaries" I see that, if you hard-code a dictionary to some values, you can use code completion when writing code about that dictionary.

但很明显,在许多情况下,您将使用 dict 并且您事先知道该 dict 的结构是什么,并且您不想将 dict 硬编码到您的代码中.例如,您可能正在解析一些具有预期结构的 YAML 或 JSON.

But obviously, in many cases, you will work with a dict and you have some idea in advance what the structure of that dict will be, and you don't want to hard-code the dict into your code. For example, maybe you're parsing some YAML or JSON that has an expected structure.

如果您能在 Python 中暗示"结构,那就太好了,这样您就可以轻松快速地对使用该字典的所有地方进行编码.这可能吗?

It would be really nice if you could "hint" the structure in Python, so you could easily and rapidly code all the places you use that dictionary. Is that possible?

推荐答案

据我所知,直到 TypedDict 是在 Python 3.8 中引入的.

As far as I'm aware, there's no agreed way to type-hint the specific key names and associated values of a Python dictionary until TypedDict is introduced in Python 3.8.

这篇关于是否可以“提示"?字典键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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