Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
usage of ":"
#1
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
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020