Why is print a keyword in python and not a function?
-
2see python.org/dev/peps/pep-3105jfs– jfs2010-12-10 22:39:12 +00:00Commented Dec 10, 2010 at 22:39
-
interesting; never considered it a problem but good question.Frank V– Frank V2010-12-10 22:41:34 +00:00Commented 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.sashoalm– sashoalm2014-04-24 11:42:13 +00:00Commented 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.steffen– steffen2016-08-19 06:05:40 +00:00Commented Aug 19, 2016 at 6:05
6 Answers
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!
5 Comments
print "foo" becomes print("foo")print was made a statement in the first place.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
It is now a function in Python 3.
1 Comment
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
end= and file= keyword parameters instead of the special syntax.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
input would need to be a statement as well.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.