• By Brett Cannon
  • Original link: http://www.snarky.ca/why-print-became-a-function-in-python-3
  • Translator: Earlgrey@programming school

In Python 2, print is a statement; In Python 3, it becomes functions. Many Python users will ask why Python 3 turned print into a function. This article is explained by Core Python developer Brett Cannon.

The decision to move Python to Github earlier this year was made by Brett Cannon after consulting the Python community. He also has an explanation.

Difference between print statement and print function

The print statement

In Python 2, the simplest form of the print statement is print A, which executes sys.stdout.write(STR (A) + ‘\n’). If you pass an extra argument with a comma delimiter, these arguments will be passed to STR (), which will eventually print with a space between each argument. For example, print A, B, C equals sys.stdout.write(“. Join (map(STR, [A, B, C])) + ‘\n’). If A comma is added at the end of the print statement, no line break (\n) is added, that is: print A is equivalent to sys.stdout.write(STR (A)).

Starting with version 2.0, Python introduced the print >> syntax, which redirects the file where the print statement ends up producing the string. For example, print >> output, A equals output.write(STR (A) + ‘\n’).

The print function

If we implemented the print function in Python, the function definition would look something like this:

import sys

def print(*objects, sep=None, end=None, file=None, flush=False):
    """A Python translation of the C code for builtins.print().


"""
    if sep is None:
        sep = ' '
    if end is None:
        end = '\n'
    if file is None:
        file = sys.stdout
    file.write(sep.join(map(str, objects)) + end)
    if flush:
        file.flush()
Copy the code

From the above code, 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=output)
Copy the code

As you can see from the example above, using the print function has obvious advantages: we can now specify separator and end string characters instead of the print statement.

The key is flexibility

The real subtlety of turning print into a function is flexibility, but it’s not easy to spot. Print as a function gives Python users and Python development teams a great deal of flexibility. For users, this lets you use print as an expression; In contrast, the print statement can only be used as a statement. For example, suppose you want to print an ellipsis at the end of each line to indicate that the line is not finished. With the print statement, you have two options:

# Manual implementation... print A, '... '# reusable implementation (this approach also applies to print functions)... def ellipsis_print(*args): for arg in args: print arg, '', print '... 'Copy the code

But in Python 3, you can choose a better solution:

# Manual implementation... print(A, end='... \n') # Multiple reusable solutions that cannot be implemented using print statements... ellipsis_print = lambda *args, **kwargs: print(*args, **kwargs, end='... \n') # or... import functools ellipsis_print = functools.partial(print, end='... \n')Copy the code

In other words, once it becomes a function, print can be componentized, and print as a statement is not supported. Alternatively, you can write your favorite print function, assign it to builtins.print, and override the built-in function implementation. This is not possible in Python 2.

For the Python development team, it is no longer necessary to implement the functions of print syntactically. For example, if you wanted print statements to have the same flexibility to support specified delimiters, how would you do that? This can be a rather difficult design problem to solve. But if print becomes a function, all you need to do is add an argument. In Python, functions can take any number of arguments, which gives more flexibility than implementing the syntax from the bottom up.

We should also note that syntactic implementations should be limited to those that have to be done this way, or those that, when implemented in syntactic form, greatly improve readability. In the case of print, the difference between print A and print(A) is negligible and therefore does not affect readability. Also, since we can completely replace print statements with functions, there is no loss of functionality to the Python language. That’s why print is a function.

Please scan the qr code below to follow my official account “programpie”, thank you for your support! I wish you all a happy New Year and a happy Year of the Monkey!