Python Forum

Full Version: usage of ":"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a question related to the : sign. I have students taking their first steps in Python. I noticed one of my students use ":" wrong. However the code runs fine.

print: float(input("Value?"))
I was wondering if anyone could clarify what "print:" does. Is it i.e. a label? In assembler this is interpreted as a label for a program location in memory.

Thanks
It is weird that this actually works. I am surprised myself that this is valid syntax.
In this case print is just a name (i.e. you can replace it with any valid python name), that in this case overrides print function
Then, in my opinion, what you have here is a type annotation or type hint (no matter how weird this may sound, given that it uses 2 function calls, but type hints are ignored by interpreter anyway, as long as they are valid expressions that evaluate without raising exceptions).

You can see that
print: float(input("Value?")) = 'spam'
print('hello')
will produce
Output:
Value?10 Traceback (most recent call last): File "c:\Users\BKolev\sandbox\urllibbk.py", line 2, in <module> print('hello') TypeError: 'str' object is not callable
Maybe others would shed more light and/or find a reference to a docs or something