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) *  |
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) *  |
Date: 2009-09-19 02:14 |
And use keyword-only arguments :)
|
msg140432 - (view) |
Author: Éric Araujo (eric.araujo) *  |
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) *  |
Date: 2013-04-01 19:15 |
Still waiting for a review.
|
msg185767 - (view) |
Author: Antoine Pitrou (pitrou) *  |
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) *  |
Date: 2013-04-02 13:29 |
Maybe pprint.print() should specify sep='\n' by default?
|
msg185872 - (view) |
Author: Éric Araujo (eric.araujo) *  |
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) *  |
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) *  |
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) *  |
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) *  |
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) *  |
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) *  |
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) *  |
Date: 2013-04-11 20:52 |
Closing as per Ezio's suggestion on #python-dev.
|
msg186594 - (view) |
Author: Ezio Melotti (ezio.melotti) *  |
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.
|
|
Date |
User |
Action |
Args |
2022-04-11 14:56:52 | admin | set | github: 50992 |
2013-04-11 20:57:01 | ezio.melotti | set | messages:
+ msg186594 |
2013-04-11 20:52:24 | pitrou | set | status: open -> closed resolution: rejected messages:
+ msg186593
|
2013-04-04 08:59:49 | giampaolo.rodola | set | messages:
+ msg186014 |
2013-04-04 06:22:42 | pitrou | set | messages:
+ msg186008 |
2013-04-04 00:43:22 | ezio.melotti | set | messages:
+ msg185984 |
2013-04-03 09:26:37 | pitrou | set | messages:
+ msg185902 |
2013-04-03 08:31:34 | amaury.forgeotdarc | set | messages:
+ msg185898 |
2013-04-03 06:10:00 | pitrou | set | messages:
+ msg185891 |
2013-04-03 01:23:48 | eric.araujo | set | messages:
+ msg185872 |
2013-04-02 14:40:39 | rhettinger | set | assignee: rhettinger |
2013-04-02 13:29:31 | amaury.forgeotdarc | set | messages:
+ msg185836 |
2013-04-01 20:46:53 | ezio.melotti | set | nosy:
+ ezio.melotti
|
2013-04-01 20:20:18 | pitrou | set | nosy:
+ pitrou messages:
+ msg185767
|
2013-04-01 19:15:14 | eric.araujo | set | keywords:
+ needs review
stage: needs patch -> patch review messages:
+ msg185761 versions:
+ Python 3.4, - Python 3.3 |
2013-04-01 18:59:39 | BreamoreBoy | set | nosy:
+ BreamoreBoy messages:
+ msg185758
|
2011-12-19 15:40:04 | pitrou | set | nosy:
+ rhettinger
|
2011-12-15 17:20:36 | giampaolo.rodola | set | nosy:
+ giampaolo.rodola
|
2011-07-15 16:00:51 | eric.araujo | set | title: 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:08 | eric.araujo | set | files:
+ pprint-print.diff keywords:
+ patch messages:
+ msg140432
stage: needs patch |
2011-03-09 02:05:16 | terry.reedy | set | nosy:
amaury.forgeotdarc, eric.araujo, marystern, serprex versions:
+ Python 3.3, - Python 3.1 |
2009-09-19 02:14:42 | eric.araujo | set | nosy:
+ eric.araujo messages:
+ msg92855
|
2009-08-22 00:16:12 | amaury.forgeotdarc | set | nosy:
+ amaury.forgeotdarc messages:
+ msg91849
|
2009-08-21 21:11:04 | serprex | set | files:
+ pprint.py nosy:
+ serprex messages:
+ msg91845
|
2009-08-20 14:26:45 | marystern | set | messages:
+ msg91776 |
2009-08-20 14:21:05 | marystern | create | |