Reprint why does print become a function in Python 3?

Source: Internet
Author: User
Tags function definition

Reprinted from the programming faction http://codingpy.com/article/why-print-became-a-function-in-python-3/

Original Brett Cannon

Original link: http://www.snarky.ca/why-print-became-a-function-in-python-3

Translator: [Email protected] programming pie

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 print statement is print a, which is equivalent to executing the sys.stdout.write (str (A) + ' \ n '). If you pass an extra parameter (argument) with a comma delimiter, these parameters are passed to the STR () function, and the final print will have a blank space between each parameter. For example, print A, B, and C are equivalent to 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 (\ n) is no longer added, which means that print A is equivalent to Sys.stdout.write (str (A)).

Starting with version 2.0, Python introduced the print >> syntax, which is to redirect the print statement to the final output string of the file. For example, print >> output, A is equivalent to Output.write (str (A) + ' \ n ').

Print function

If you use Python to implement the print function, its function definition should look 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 ()

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=output)

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 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 (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, **kwargs, end= ' ... \ n ')

# or ... or.

Import Functools

Ellipsis_print = functools.partial (print, end= ' ... \ n ')

In other words, after becoming a function, print can be modular, and print as a statement is not supported. Also, you can write your favorite print function, assign it to Builtins.print, and you can overwrite your own function implementation. This is not possible in Python 2.

For the Python development team, they no longer have to implement print-related functionality from a syntactic level. For example, what do you do if you want the print statement to have the same flexibility to support the specified delimiter? 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, the difference between print A and print (a) is negligible and therefore does not affect readability. Also, because we can completely replace the print statement with a function, there is no loss in the functionality of the Python language. That's why you turn print into a function.

Reprint why does print become a function in Python 3?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.