Why is print not a function in python?

前端 未结 6 1994
情深已故
情深已故 2021-02-13 13:21

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

相关标签:
6条回答
  • 2021-02-13 13:29

    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.

    0 讨论(0)
  • 2021-02-13 13:34

    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.

    0 讨论(0)
  • 2021-02-13 13:36

    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.

    0 讨论(0)
  • 2021-02-13 13:38

    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.

    0 讨论(0)
  • 2021-02-13 13:48

    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!

    0 讨论(0)
  • 2021-02-13 13:49

    It is now a function in Python 3.

    0 讨论(0)
提交回复
热议问题