Why is print not a function in python?

前端 未结 6 2015
情深已故
情深已故 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.

提交回复
热议问题