Mike Mike - 4 months ago 7
Python Question

What is 'print' in Python?

I understand what

print
does, but of what "type" is that language element? I think it's a function, but why does this fail?

>>> print print
SyntaxError: invalid syntax


Isn't
print
a function? Shouldn't it print something like this?

>>> print print
<function print at ...>

wim wim
Answer

In 2.7 and down, print is a statement. In python 3, print is a function. To use the print function in Python 2.6 or 2.7, you can do

>>> from __future__ import print_function
>>> print(print)
<built-in function print>

See this section from the Python Language Reference, as well as PEP 3105 for why it changed.