61

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 ...>
1
  • print may be a function or a statement (language construct), depending on the major version of Python you're using, but it's definitely not an operator. Commented Aug 5, 2016 at 17:12

5 Answers 5

63

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.

Sign up to request clarification or add additional context in comments.

1 Comment

About time they fixed that. The broken print statement is the #1 reason why haven't used Python in the past.
35

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.

4 Comments

In Python 3 OP will get the same output, albeit for a different reason; print print is bad syntax in both.
@Wooble: Absolutely. Being a function, print() requires brackets. I inclued these in my answer.
Yes, I was just referring to "You must be using python < 3". The error message is literally identical to the OP's in both.
@Downvoter: I'm curious as to which parts of my answer you deemed "not useful".
21

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.

Comments

7

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

3 Comments

"verboten" is used in english? funny.
@cularis: the word probably became more commonly used as a loanword because of American movies about World War 2.
Nothing confusing about that; in Python 2.6+ there is built-in print function all the time. from __future__ import print_function removes the print-the-statement parsing from the module, so that it doesn't interfere with the built-in function.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.