classification
Title: Add function compatible with print to pprint module
Type: enhancement Stage: patch review
Components: Versions: Python 3.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: BreamoreBoy, amaury.forgeotdarc, eric.araujo, ezio.melotti, giampaolo.rodola, marystern, pitrou, rhettinger, serprex
Priority: normal Keywords: needs review, patch

Created on 2009-08-20 14:21 by marystern, last changed 2013-04-11 20:57 by ezio.melotti. This issue is now closed.

Files
File name Uploaded Description Edit
pprint.py serprex, 2009-08-21 21:11 Patched pprint
pprint-print.diff eric.araujo, 2011-07-15 16:00 review
Messages (19)
msg91774 - (view) Author: Mary Stern (marystern) Date: 2009-08-20 14:21
Using print in python 3 I would like to simple "replace" print with
pprint.pprint. However pprint cannot be called with no arguments, so
this cannot currently be done (the error is "TypeError: pprint() takes
at least 1 positional argument (0 given)").

A simple improvement is to allow no object to be passed in and pprint
would then print a newline rather than fail.

Another problem is that if you try this:

 print('hello', True)

and replace print with print, the second arg gets interpreted as the
"stream" parameter.


Both of the above can be fixed (I think) by changing pprint.py as follows:

instead of the current code:
def pprint(object, stream=None, indent=1, width=80, depth=None):

change to this:
def pprint(object='\n', *args, stream=None, indent=1, width=80, depth=None)
msg91776 - (view) Author: Mary Stern (marystern) Date: 2009-08-20 14:26
Sorry: you also need to print out the args! :) .. like this:

def pprint(object='\n', *args, stream=None, indent=1, width=80, depth=None):
    """Pretty-print a Python object to a stream [default is sys.stdout]."""
    printer = PrettyPrinter(
        stream=stream, indent=indent, width=width, depth=depth)
    printer.pprint(object)
    for arg in args:
        printer.pprint(arg)
msg91845 - (view) Author: Demur Rumed (serprex) Date: 2009-08-21 21:11
I've included a patched version. I went with using a function signature
of def pprint(*object, stream=None, indent=1, width=80, depth=None)
msg91849 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-08-22 00:16
Changing the signature of a well-used function is dangerous.
Why not add a function named pprint.print, with the exact same signature 
as __builtin__.print?
msg92855 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2009-09-19 02:14
And use keyword-only arguments :)
msg140432 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-07-15 16:00
Here’s a first draft at a patch.  I’ve added a module-level print function and a PrettyPrinter.print method which does the real work.

The functions have a signature compatible with print, but I have changed one default value: sep defaults to ',\n', because I think that the output is more useful and more pleasing (otherwise you’d have strange-looking indentation with containers).

If this looks good, I’ll work on doc and tests.  I guess I’ll reuse the helpers in test_print.
msg185758 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2013-04-01 18:59
@Éric I'd be interested in using this as print/pprint are part of my debugging kit, so could you take this forward please?
msg185761 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2013-04-01 19:15
Still waiting for a review.
msg185767 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-04-01 20:20
I don't understand how this is supposed to work or what the point is. By construction, pprint prints a *single* object, and it will split the representation over several lines if necessary. Therefore the print() semantics (print several objects one after another, without linebreak) are incompatible with pprint.
msg185836 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2013-04-02 13:29
Maybe pprint.print() should specify sep='\n' by default?
msg185872 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2013-04-03 01:23
Antoine: the desired behavior is to have a function with the same signature as print (so multiple objects to print are supported), but which calls pprint.pformat instead of str.  It lets people monkey-patch builtins.print or somemodule.print with pprint.print and get nicer output.  (I rarely use print these days, as I work in web applications that use logging and responses, but in many other cases people use print and might want a quick way to make it pretty-print.)
msg185891 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-04-03 06:10
> Antoine: the desired behavior is to have a function with the same
> signature as print (so multiple objects to print are supported), but
> which calls pprint.pformat instead of str.

But it can't work. pprint() uses all the width for a single object. How
are you supposed to print multiple objects?
msg185898 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2013-04-03 08:31
> But it can't work. pprint() uses all the width for a single object.
> How are you supposed to print multiple objects?

On multiple lines?
msg185902 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-04-03 09:26
> Amaury Forgeot d'Arc added the comment:
> 
> > But it can't work. pprint() uses all the width for a single object.
> > How are you supposed to print multiple objects?
> 
> On multiple lines?

But then, it doesn't have anything similar to print() except that it takes
multiple arguments (which is a very weak similarity). It can only be confusing
to name it print(), not to mention that shadowing builtins is generally
considered bad.

Overall, -1 from me.
msg185984 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-04-04 00:43
> But then, it doesn't have anything similar to print()

It has the same signature.  Isn't that the point of the whole issue?
msg186008 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-04-04 06:22
> > But then, it doesn't have anything similar to print()
> 
> It has the same signature.  Isn't that the point of the whole issue?

This is becoming stupid. How about making it the same signature as sum()
or max() too?
msg186014 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2013-04-04 08:59
-1 from me as well. Such kind of debatable (in terms of usefulness and inclusion) things are usually better off being served by a custom function in user's code.
msg186593 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-04-11 20:52
Closing as per Ezio's suggestion on #python-dev.
msg186594 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-04-11 20:57
To clarify, the request seems reasonable to me, but the use cases are somewhat limited and it's not obvious what the best implementation should be.  Given that others are -1 on this, I suggested to close it rather than leaving it open and languishing.
History
Date User Action Args
2013-04-11 20:57:01ezio.melottisetmessages: + msg186594
2013-04-11 20:52:24pitrousetstatus: open -> closed
resolution: rejected
messages: + msg186593
2013-04-04 08:59:49giampaolo.rodolasetmessages: + msg186014
2013-04-04 06:22:42pitrousetmessages: + msg186008
2013-04-04 00:43:22ezio.melottisetmessages: + msg185984
2013-04-03 09:26:37pitrousetmessages: + msg185902
2013-04-03 08:31:34amaury.forgeotdarcsetmessages: + msg185898
2013-04-03 06:10:00pitrousetmessages: + msg185891
2013-04-03 01:23:48eric.araujosetmessages: + msg185872
2013-04-02 14:40:39rhettingersetassignee: rhettinger
2013-04-02 13:29:31amaury.forgeotdarcsetmessages: + msg185836
2013-04-01 20:46:53ezio.melottisetnosy: + ezio.melotti
2013-04-01 20:20:18pitrousetnosy: + pitrou
messages: + msg185767
2013-04-01 19:15:14eric.araujosetkeywords: + needs review

stage: needs patch -> patch review
messages: + msg185761
versions: + Python 3.4, - Python 3.3
2013-04-01 18:59:39BreamoreBoysetnosy: + BreamoreBoy
messages: + msg185758
2011-12-19 15:40:04pitrousetnosy: + rhettinger
2011-12-15 17:20:36giampaolo.rodolasetnosy: + giampaolo.rodola
2011-07-15 16:00:51eric.araujosettitle: pprint.pprint should support no objects to print blank lines & allow args -> Add function compatible with print to pprint module
2011-07-15 16:00:08eric.araujosetfiles: + pprint-print.diff
keywords: + patch
messages: + msg140432

stage: needs patch
2011-03-09 02:05:16terry.reedysetnosy: amaury.forgeotdarc, eric.araujo, marystern, serprex
versions: + Python 3.3, - Python 3.1
2009-09-19 02:14:42eric.araujosetnosy: + eric.araujo
messages: + msg92855
2009-08-22 00:16:12amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg91849
2009-08-21 21:11:04serprexsetfiles: + pprint.py
nosy: + serprex
messages: + msg91845

2009-08-20 14:26:45marysternsetmessages: + msg91776
2009-08-20 14:21:05marysterncreate