Because Guido has decided that he made a mistake. :)
It has since been corrected: try Python 3, which dedicates a section of its release notes to describing the change to a function.
For the whole background, see PEP 3105 and the several links provided in its References section!
Answer from Brandon Rhodes on Stack Overflow
What are the pros and cons to having print being a keyword versus print() being a built-in function?
why is print a legal variable name?
Should print be a keyword (`print "foo"`), or a built-in function (`print("hello")`)?
Because Guido has decided that he made a mistake. :)
It has since been corrected: try Python 3, which dedicates a section of its release notes to describing the change to a function.
For the whole background, see PEP 3105 and the several links provided in its References section!
print was a statement in Python because it was a statement in ABC, the main inspiration for Python (although it was called WRITE there). That in turn probably had a statement instead of a function as it was a teaching language and as such inspired by basic. Python on the other hand, turned out to be more than a teaching language (although it's good for that too).
However, nowadays print is a function. Yes, in Python 2 as well, you can do
from __future__ import print_function
and you are all set. Works since Python 2.6.
Here you can read some official documentation with examples and PEP3105 with more details.
As quick differences, there are several things which are very easy to do with print function (Python 3 or from __future__ import print_function) or at least very consistently:
No newline
print('Hello!', end='')
That was more complicated in python 2.
Print to stderr
import sys
print('ERROR!!!!!', file=sys.stderr)
In python 2.
Not really. With the function you must do print(stuff), but with the statement you can do print stuff.
The main advantage is that, if print is a function, you can assign a different function to that name. For instance, you could create a new function that not only outputs the data to the screen but also logs it to a file. If you assign this function to the builtin print, then any code that uses print will automatically log to a file. This is not possible with print as a statement, and you must resort to other techniques to get the same effect.
There are also some smaller changes associated with the way print formats its output. The statement used "magic" syntax (e.g., a trailing comma suppressed a following space), while the function uses standard keyword-argument conventions.
Also, print is always a function in Python 3. It's in Python 2 that you must use the __future__ import if you want print as a function.
I was quizzed on Python, and asked if "print" was a legal variable name. I thought it was not a legal variable name, but it is. But, when used as a variable name, the ability to use the print function is lost. Why would python allow that usage?
print=3
x=print
print(x)
Traceback (most recent call last):
File "G:/PYTHON/Projects/printasvariable.py", line 3, in <module>
print(x)
TypeError: 'int' object is not callable
>>>