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

I have heard several times that the print (3.x) function is better than the expression (2.x). But why?

I was a fan of this statement mainly because it allowed me to type two more characters (i.e. parentheses).

I would be interested to see some situations where the print statement simply does not shorten it and the function is superior.

+64
function python printing
Jun 04 2018-11-11T00:
source share
5 answers

Everything from Jochen to Sven's answer , plus:

You can use print() in places where you cannot use print , for example:

 [print(x) for x in range(10)] 
+40
Jun 05 2018-11-11T00:
source share
β€” -

rationale

The printed expression has long appeared on lists of dubious language features that should be removed in Python 3000, such as Guido's "Python Regrets" presentation [1] . So the goal of this PEP is not new, although it may be a subject of controversy among Python developers.

The following arguments to the print () function are taken from the python-3000 post by Guido himself [2] :

  • print is the only application level functionality for which there is a dedicated statement. In the Python world, syntax is usually used as a last resort when something cannot be done without the help of a compiler. Printing is not suitable for such an exception.
  • At some point in application development, it is very often necessary to replace print output with something more complicated, for example, recording calls or calls to some other I / O library. With the print () function, this is a simple line replacement, today it is a mess adding all these brackets and possibly converting >> stream style syntax.
  • The presence of special syntax for printing creates a much greater barrier to evolution, for example, the hypothetical new function printf () is not chosen too far when it coexists with the print () function.
  • There is no easy way to convert print statements to another call if you need a different separator, with no spaces or no spaces at all. In addition, there is no easy way to conveniently print objects with a separator other than a space.
  • If print () is a function, it would be much easier to replace it in one module (just def print(*args):... ) or even in the whole program (for example, by placing another function in __builtin__.print ). Actually, this can be done by writing a class using the write () method and assigning it to sys.stdout - this is not bad, but certainly a much bigger conceptual leap, and it works at a different level than print

- PEP 3105 - make print function

+52
Jun 04 2018-11-11T00:
source share

One of the benefits of print as a function is consistency. There is no reason for this statement. Compare these two lines

 2.x: print >> my_file, x 3.x: print(x, file=my_file) 

The new version is more like Python, isn't it?

Another advantage of the feature version is flexibility. For example, if you want to catch all print calls for debugging purposes, now you can simply override print :

 def print(*args, **kwargs): # whatever __builtins__.print(*args, **kwargs) 
+32
Jun 04 2018-11-11T00:
source share

I thought about this question and had no idea about the pros of the python 3 version. But when I needed to print pandas.DataFrame columns (without Index([...]) ), I found that

  print * df.columns 

throws an exception, and

  print (* df.columns) 

works great! And if you want to have the same (customizable) print options in several reviews, you can save them in the dictionary and pass them as **print_options

So, at least *args , **kw_args tricks are a good reason to print as a function!

+18
Jun 26 '14 at 19:00
source share

You can replace the built-in print with a custom print :

 import os import sys def print(s): sys.stderr.write('Will now print ' + str(s) + '.' + os.linesep) sys.stdout.write(str(s) + os.linesep) print(['A', 'list']) # Output: # stderr: Will now print ['A', 'list']. # stdout: ['A', 'list'] 

You can use print inside a lambda or function call, etc .:

 example_timeout_function(call=lambda: print('Hello world'), timeout=5) do_things(print_function=print) 
+11
Jun 04 2018-11-11T00:
source share



All Articles