@@ -33,7 +33,7 @@ The :mod:`pprint` module defines one class:
3333 .. index :: single: ...; placeholder
3434
3535.. class :: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \
36- compact=False)
36+ compact=False, sort_dicts=True )
3737
3838 Construct a :class: `PrettyPrinter ` instance. This constructor understands
3939 several keyword parameters. An output stream may be set using the *stream *
@@ -50,11 +50,17 @@ The :mod:`pprint` module defines one class:
5050 structure cannot be formatted within the constrained width, a best effort will
5151 be made. If *compact * is false (the default) each item of a long sequence
5252 will be formatted on a separate line. If *compact * is true, as many items
53- as will fit within the *width * will be formatted on each output line.
53+ as will fit within the *width * will be formatted on each output line. If
54+ *sort_dicts * is true (the default), dictionaries will be formatted with their
55+ keys sorted, otherwise they will display in insertion order.
5456
5557 .. versionchanged :: 3.4
5658 Added the *compact * parameter.
5759
60+ .. versionchanged :: 3.8
61+ Added the *sort_dicts * parameter.
62+
63+
5864 >>> import pprint
5965 >>> stuff = [' spam' , ' eggs' , ' lumberjack' , ' knights' , ' ni' ]
6066 >>> stuff.insert(0 , stuff[:])
@@ -81,29 +87,47 @@ The :mod:`pprint` module defines one class:
8187
8288The :mod: `pprint ` module also provides several shortcut functions:
8389
84- .. function :: pformat(object, indent=1, width=80, depth=None, *, compact=False)
90+ .. function :: pformat(object, indent=1, width=80, depth=None, *, \
91+ compact=False, sort_dicts=True)
8592
8693 Return the formatted representation of *object * as a string. *indent *,
87- *width *, *depth * and *compact * will be passed to the :class: ` PrettyPrinter `
88- constructor as formatting parameters.
94+ *width *, *depth *, * compact * and *sort_dicts * will be passed to the
95+ :class: ` PrettyPrinter ` constructor as formatting parameters.
8996
9097 .. versionchanged :: 3.4
9198 Added the *compact * parameter.
9299
100+ .. versionchanged :: 3.8
101+ Added the *sort_dicts * parameter.
102+
103+
104+ .. function :: pp(object, *args, sort_dicts=False, **kwargs)
105+
106+ Prints the formatted representation of *object * followed by a newline.
107+ If *sort_dicts * is false (the default), dictionaries will be displayed with
108+ their keys in insertion order, otherwise the dict keys will be sorted.
109+ *args * and *kwargs * will be passed to :func: `pprint ` as formatting
110+ parameters.
111+
112+ .. versionadded :: 3.8
113+
93114
94115.. function :: pprint(object, stream=None, indent=1, width=80, depth=None, *, \
95- compact=False)
116+ compact=False, sort_dicts=True )
96117
97118 Prints the formatted representation of *object * on *stream *, followed by a
98119 newline. If *stream * is ``None ``, ``sys.stdout `` is used. This may be used
99120 in the interactive interpreter instead of the :func: `print ` function for
100121 inspecting values (you can even reassign ``print = pprint.pprint `` for use
101- within a scope). *indent *, *width *, *depth * and *compact * will be passed
102- to the :class: `PrettyPrinter ` constructor as formatting parameters.
122+ within a scope). *indent *, *width *, *depth *, *compact * and * sort_dicts * will
123+ be passed to the :class: `PrettyPrinter ` constructor as formatting parameters.
103124
104125 .. versionchanged :: 3.4
105126 Added the *compact * parameter.
106127
128+ .. versionchanged :: 3.8
129+ Added the *sort_dicts * parameter.
130+
107131 >>> import pprint
108132 >>> stuff = [' spam' , ' eggs' , ' lumberjack' , ' knights' , ' ni' ]
109133 >>> stuff.insert(0 , stuff)
0 commit comments