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 ...>
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.
In Python 3, print()
is a built-in function (object)
Before this, print
was a statement. Demonstration...
Python 2.x:
% pydoc2.6 print
The ``print`` statement
***********************
print_stmt ::= "print" ([expression ("," expression)* [","]]
| ">>" expression [("," expression)+ [","]])
``print`` evaluates each expression in turn and writes the resulting
object to standard output (see below). If an object is not a string,
it is first converted to a string using the rules for string
conversions. The (resulting or original) string is then written. A
space is written before each object is (converted and) written, unless
the output system believes it is positioned at the beginning of a
line. This is the case (1) when no characters have yet been written
to standard output, (2) when the last character written to standard
output is a whitespace character except ``' '``, or (3) when the last
write operation on standard output was not a ``print`` statement. (In
some cases it may be functional to write an empty string to standard
output for this reason.)
-----8<-----
Python 3.x:
% pydoc3.1 print
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
print
is a mistake that has been rectified in Python 3. In Python 3 it is a function. In Python 1.x and 2.x it is not a function, it is a special form like if
or while
, but unlike those two it is not a control structure.
So, I guess the most accurate thing to call it is a statement.
In Python all statements (except assignment) are expressed with reserved words, not addressible objects. That is why you cannot simply print print
and you get a SyntaxError
for trying. It's a reserved word, not an object.
Confusingly, you can have a variable named print
. You can't address it in the normal way, but you can setattr(locals(), 'print', somevalue)
and then print locals()['print']
.
Other reserved words that might be desirable as variable names but are nonetheless verboten:
class
import
return
raise
except
try
pass
lambda
In Python 2, print
is a statement, which is a whole different kind of thing from a variable or function. Statements are not Python objects that can be passed to type()
; they're just part of the language itself, even more so than built-in functions. For example, you could do sum = 5
(even though you shouldn't), but you can't do print = 5
or if = 7
because print
and if
are statements.
In Python 3, the print
statement was replaced with the print()
function. So if you do type(print)
, it'll return <class 'builtin_function_or_method'>
.
BONUS:
In Python 2.6+, you can put from __future__ import print_function
at the top of your script (as the first line of code), and the print
statement will be replaced with the print()
function.
>>> # Python 2
>>> from __future__ import print_function
>>> type(print)
<type 'builtin_function_or_method'>