简体   繁体   中英

Python - curly braces in type hints

What do these mean?

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

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?

The particular example where I've seen this is in tkinter 's Frame __init__ method, where the master parameter is of type {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'

I coded up a minimal example in pycharm:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM