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}
.
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 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.
In fact, Python supports curly braces, BEGIN/END, and almost any other language's block schemes: see python.org/doc/humor/…!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With