Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why interpreter is not throwing error if I add colon after print? [duplicate]

I add colon and semicolon after print but interpreter is not throwing error.

please run with python3.8.x(edit)

x=5
print:(x)
print;(x)
like image 734
Ratnapal Shende Avatar asked Mar 02 '23 04:03

Ratnapal Shende


1 Answers

The interpreter thinks the colon is a type annotation. Which is why it raises SyntaxError in earlier versions of Python, but is valid syntax in Python 3.6+.

In later versions of Python this is valid

a: int

As is this

import sys

def exclaim(string):
    sys.stdout.write(f"{string}!")

print = exclaim
print("Hello")

I.e. You can annotate the type of a variable. And you can reassign print.

So when you do print:(x) the interpreter just thinks you're annotating print to be of "type" 5.

Semi-colons are valid Python, and are used to put two separate statements on the same line. They're just considered to by "unpythonic". You do see them used sometimes to do things like import pdb; pdb.set_trace()

like image 120
Batman Avatar answered Apr 25 '23 03:04

Batman