14

Why is print a keyword in python and not a function?

4
  • 2
    see python.org/dev/peps/pep-3105 Commented Dec 10, 2010 at 22:39
  • interesting; never considered it a problem but good question. Commented Dec 10, 2010 at 22:41
  • readln and writeln were built-ins in Turbo Pascal as well. Never understood why, and I liked C better for making them functions instead. Commented Apr 24, 2014 at 11:42
  • @JFSebastian: That explains, why a print function would be better. But not, why it wasn't one in the first place. Commented Aug 19, 2016 at 6:05

6 Answers 6

14

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!

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

5 Comments

I havent tinkered with Python 3 yet. Does the print keyword still work?
No. It's a function now. In the simple case, print "foo" becomes print("foo")
Corrected as in "now a function" :)
@steffen — I have added a direct link to the PEP, and fixed the name of the first link to make clearer that I am linking directly to the release notes explanation of the change.
@BrandonRhodes: My confusion is rather due to the acceptance of your answer as the correct one by the OP than your answer itself. I tend to agree with you that it was a mistake in the first place and has been corrected in Python3. But the original question was why print was made a statement in the first place.
8

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.

Comments

2

It is now a function in Python 3.

1 Comment

@user: It's not necessarily a matter of time so much as a matter of compatibility, both forwards and backwards.
1

The print statement in Python 2.x has some special syntax which would not be available for an ordinary function. For example you can use a trailing , to suppress the output of a final newline or you can use >> to redirect the output to a file. But all this wasn't convincing enough even to Guido van Rossum himself to keep it a statement -- he turned print into a function in Python 3.x.

3 Comments

Uhhh, now this is starting to make sense.
So the additional functionality is lost now though? Such as the trailing , and >>
It's still there, but now you use the end= and file= keyword parameters instead of the special syntax.
1

An answer that draws from what I appreciate about the print statement, but not necessarily from the official Python history...

Python is, to some extent, a scripting language. Now, there are lots of definitions of "scripting language", but the one I'll use here is: a language designed for efficient use of short or interactive programs. Such languages tend to allow one-line programs without excessive boilerplate; make keyboard input easier (for instance, by avoiding excessive punctuation); and provide built-in syntax for common tasks (convenience at the possible expense of purity). In Python's case, printing a value is a very common thing to do, especially in interactive mode. Requiring print to be a function seems unnecessarily inconvenient here. There's a significantly lower risk of error with the special syntax that does the right thing 99% of the time.

1 Comment

By this argument input would need to be a statement as well.
0

I will throw in my thoughts on this:

In Python 2.x print is not a statement by mistake, or because printing to stdout is such a basic thing to do. Everything else is so thought-through or has at least understandable reasons that a mistake of that order would seem odd. If communicating with stdout would have been cosidered so basic, communicating with stdin would have to be just as important, yet input() is a function.

If you look at the list of reserved keywords and the list of statements which are not expressions, print clearly stands out which is another hint that there must be very specific reasons.

I think print had to be a statement and not an expression, to avoid a security breach in input(). Remember that input() in Python2 evaluates whatever the user types into stdin. If the user typed print a and a holds a list of all passwords, that would be quiet catastrophic.

Apparently, the ability of input() to evaluate expressions was considered more important than print being a normal built-in function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.