Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}.

like image 535
Enn Michael Avatar asked Aug 01 '17 22:08

Enn Michael


People also ask

What does this {} mean in Python?

In languages like C curly braces ( {} ) are used to create program blocks used in flow control. In Python, curly braces are used to define a data structure called a dictionary (a key/value mapping), while white space indentation is used to define program blocks.

What does {} brackets mean in Python?

What do {} bracket mean in Python? [] brackets are used for lists. List contents can be changed, unlike tuple content. {} are used to define a dictionary in a “list” called a literal.

Can we use {} in Python?

In fact, Python supports curly braces, BEGIN/END, and almost any other language's block schemes: see python.org/doc/humor/…!


1 Answers

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:

enter image description here

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.

like image 58
Doug Coburn Avatar answered Oct 21 '22 04:10

Doug Coburn