Transferred from: http://www.codingpy.com/article/why-print-became-a-function-in-python-3/
In Python 2, print is a statement (statement), and in Python 3 it becomes a function. Many Python users ask, why is Python 3 turning print into a function? This is the explanation for the Python core developer, Brett Cannon.
Earlier this year Python decided to move to GitHub, which was made by Brett Cannon, who consulted the Python community. He also explained that.
The difference between the print statement and the PRINT function print statement
In Python 2, the simplest form of use of the print statement is that print A
it is equivalent to executing sys.stdout.write(str(A) + ‘\n‘)
. If you pass the extra arguments (argument) with a comma delimiter, the arguments are passed to the str()
function, and the final print will be empty between each parameter. For example, the print A, B, C
equivalent sys.stdout.write(‘ ‘.join(map(str, [A, B, C])) + ‘\n‘)
. If you add a comma at the end of the print statement, the break character () is no longer added \n
, which means: print A
equivalent sys.stdout.write(str(A))
.
Starting with version 2.0, Python introduced print >>
the syntax for redirecting print
statements to the final output string of a file. For example, the print >> output, A
equivalent output.write(str(A) + ‘\n‘)
.
Print function
If you use Python to implement the print function, its function definition should look like this:
ImportSysDefPrint(*Objects,Sep=None,End=None,File=None,Flush=False):"" A Python translation of the C Code for Builtins.print ()."""IfSepIsNone:Sep=‘ ‘IfEndIsNone:End= ' \n ' if file Span class= "ow" >is none: file = Sys. StdOut file. Write (sep. Join (mapstrobjects+ end) if flush: Span class= "NB" >file. Flush ()
From the code above, we can see that the print function in Python 3 implements all the features of the print statement.
Print a = = Print (a) print a, b, c = Print (A, B, c) print a, = = Print (A, end= ") print >> output, a = = Print (A, File=ou Tput
As we can see from the example code above, there are obvious advantages to using the Print function: We can now specify additional delimiters (separator) and terminator (end string) compared to using the print statement.
The key is flexibility.
The true ingenuity and flexibility of turning print into a function is not easily noticed. After print becomes a function, it gives Python users and the Python development team a lot of flexibility. For the user, this allows you to use it as print
an expression, by contrast, the print statement can only be used as a statement. For example, suppose you want to print an ellipsis (ellipsis) after each line, indicating that the line is not yet finished. With the print statement, you have two options:
# Manual Implementation ... print A, ' ... ' # REUSABLE implementations (this way also applies to the Print function) ... def ellipsis_print (*args): for arg in args: print arg, ',
print ' ... '
But in Python 3, you can choose a better solution:
# Manual Implementation ...Print(A,End=‘...\ n‘)# Multiple reusable solutions that cannot be implemented with print statements ...Ellipsis_print=Lambda*args**kwargs : print (*args* *kwargsend=\n ") # or ... import functoolsellipsis_print = functools. Partial (printend= ... \n ")
In other words, after becoming a function, print
it can be a component, which print
is not supported as a statement. Also, you can write your favorite print
functions and assign them to them, and you builtins.print
can overwrite the function implementations. This is not possible in Python 2.
For the Python development team, they do not have to implement print
the relevant functionality from the syntactic level. For example, if you want print
the statement to have the same flexibility to support the specified delimiter, how do you implement it? This can be a rather difficult design problem to solve. But if print becomes a function, just add a new parameter to solve it. In Python, a function can accept any number of parameters, which is much more flexible than the underlying implementation syntax.
We should also note that the syntax implementation should be limited to those functions that do not do so, or are implemented in a syntactic form, greatly improving the readability of the function. In print
this case, print A
print(A)
the difference between the and is negligible and therefore does not affect readability. Also, since we can completely print
replace the statement with a function, there is no loss in the functionality of the Python language. That is why it print
becomes a function.
Why does print become a function in Python3?