Why is print
a keyword in python and not a function?
I will throw in my thoughts on this:
In Python 2.x print
is not a statement by mistake, or because printing to stdout
is such a basic thing to do. Everything else is so thought-through or has at least understandable reasons that a mistake of that order would seem odd. If communicating with stdout
would have been cosidered so basic, communicating with stdin
would have to be just as important, yet input()
is a function.
If you look at the list of reserved keywords and the list of statements which are not expressions, print
clearly stands out which is another hint that there must be very specific reasons.
I think print
had to be a statement and not an expression, to avoid a security breach in input()
. Remember that input()
in Python2 evaluates whatever the user types into stdin
. If the user typed print a
and a
holds a list of all passwords, that would be quiet catastrophic.
Apparently, the ability of input()
to evaluate expressions was considered more important than print
being a normal built-in function.