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'>