Python - 类型提示中的花括号 [英] Python - curly braces in type hints

查看:64
本文介绍了Python - 类型提示中的花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是什么意思?

 def f(a: {int, float}):
    pass

在通过 PyCharm 获取文档时,我已经在一些标准 Python 模块中看到了这种语法,但我不知道它的含义.在我的示例中 a 的提示类型是什么?我可以向这个函数传递什么类型?

I've seen this syntax used in some standard Python modules when fetching documentation via PyCharm, and I have no idea what it means. What's the hinted type for a in my example? What types can I pass to this function?

我在 tkinterFrame __init__ 方法中看到的特定示例,其中 master 参数的类型为 {tk, _w}.

The particular example where I've seen this is in tkinter's Frame __init__ method, where the master parameter is of type {tk, _w}.

推荐答案

这是一个提示,告诉您它想要一个具有命名属性 'int' 和 'float' 的对象——或者更具体地用于 tkinter 'tk' 和 '_w'

It's a hint telling you it wants an object with the named attributes 'int' and 'float' -- or more specifically for tkinter 'tk' and '_w'

我在 pycharm 中编写了一个最小的例子:

I coded up a minimal example in pycharm:

检查 python 库 sources -- 你可以看到有对master.tkmaster._w 的尝试访问.这就是 pycharm 能够推断出参数 master 类型的全部内容,因此它以这种方式将其浮动到 IDE.

Inpecting the python library sources -- You can see that there are attempted accesses to master.tk and master._w. That's all that pycharm was able to infer about the type of the parameter master so it floated it up to the IDE in this manner.

这篇关于Python - 类型提示中的花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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