Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of the new print function in Python 3.x over the Python 2 print statement?

I've heard several times that print being a function (3.x) is better than it being a statement (2.x). But why?

I was a fan of it being a statement mainly because it allowed me to type two less characters (ie, the parentheses).

I'd be interested to see some situations where the print statement just doesn't cut it, and a function is superior.

like image 827
rectangletangle Avatar asked Jun 04 '11 22:06

rectangletangle


People also ask

Why is print a function in Python 3?

Python print() Function The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

Why is Python's print a statement rather than a function or expression?

This is because they both have a function defined called print that takes in a string argument and prints it. Python2 also allowed you to make a statement to print to standard out without calling a function.

What is the difference between print and print in Python?

print(' ') will print a space character and a newline; while print('') (or print() ) will only print the newline. It doesn't take away anything. Thank you very much!


2 Answers

Rationale

The print statement has long appeared on lists of dubious language features that are to be removed in Python 3000, such as Guido's "Python Regrets" presentation [1]. As such, the objective of this PEP is not new, though it might become much disputed among Python developers.

The following arguments for a print() function are distilled from a python-3000 message by Guido himself [2]:

  • print is the only application-level functionality that has a statement dedicated to it. Within Python's world, syntax is generally used as a last resort, when something can't be done without help from the compiler. Print doesn't qualify for such an exception.
  • At some point in application development one quite often feels the need to replace print output by something more sophisticated, like logging calls or calls into some other I/O library. With a print() function, this is a straightforward string replacement, today it is a mess adding all those parentheses and possibly converting >>stream style syntax.
  • Having special syntax for print puts up a much larger barrier for evolution, e.g. a hypothetical new printf() function is not too far fetched when it will coexist with a print() function.
  • There's no easy way to convert print statements into another call if one needs a different separator, not spaces, or none at all. Also, there's no easy way at all to conveniently print objects with some other separator than a space.
  • If print() is a function, it would be much easier to replace it within one module (just def print(*args):...) or even throughout a program (e.g. by putting a different function in __builtin__.print). As it is, one can do this by writing a class with a write() method and assigning that to sys.stdout – that's not bad, but definitely a much larger conceptual leap, and it works at a different level than print.

— PEP 3105 – Make print a function

like image 158
Jochen Ritzel Avatar answered Oct 16 '22 03:10

Jochen Ritzel


Everything from Jochen's answer and Sven's answer, plus:

You can use print() it in places where you can't use print, such as:

[print(x) for x in range(10)] 
like image 35
Lennart Regebro Avatar answered Oct 16 '22 01:10

Lennart Regebro