gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

732 lines
59KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="pprint — Data pretty printer" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/pprint.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/pprint.py The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted struc..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/pprint.py The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted struc..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>pprint — Data pretty printer &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="reprlib — Alternate repr() implementation" href="reprlib.html" />
  33. <link rel="prev" title="copy — Shallow and deep copy operations" href="copy.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/pprint.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code> — Data pretty printer</a><ul>
  86. <li><a class="reference internal" href="#functions">Functions</a></li>
  87. <li><a class="reference internal" href="#prettyprinter-objects">PrettyPrinter Objects</a></li>
  88. <li><a class="reference internal" href="#example">Example</a></li>
  89. </ul>
  90. </li>
  91. </ul>
  92. </div>
  93. <div>
  94. <h4>Previous topic</h4>
  95. <p class="topless"><a href="copy.html"
  96. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copy</span></code> — Shallow and deep copy operations</a></p>
  97. </div>
  98. <div>
  99. <h4>Next topic</h4>
  100. <p class="topless"><a href="reprlib.html"
  101. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> — Alternate <code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code> implementation</a></p>
  102. </div>
  103. <div role="note" aria-label="source link">
  104. <h3>This Page</h3>
  105. <ul class="this-page-menu">
  106. <li><a href="../bugs.html">Report a Bug</a></li>
  107. <li>
  108. <a href="https://github.com/python/cpython/blob/main/Doc/library/pprint.rst"
  109. rel="nofollow">Show Source
  110. </a>
  111. </li>
  112. </ul>
  113. </div>
  114. </nav>
  115. </div>
  116. </div>
  117. <div class="related" role="navigation" aria-label="related navigation">
  118. <h3>Navigation</h3>
  119. <ul>
  120. <li class="right" style="margin-right: 10px">
  121. <a href="../genindex.html" title="General Index"
  122. accesskey="I">index</a></li>
  123. <li class="right" >
  124. <a href="../py-modindex.html" title="Python Module Index"
  125. >modules</a> |</li>
  126. <li class="right" >
  127. <a href="reprlib.html" title="reprlib — Alternate repr() implementation"
  128. accesskey="N">next</a> |</li>
  129. <li class="right" >
  130. <a href="copy.html" title="copy — Shallow and deep copy operations"
  131. accesskey="P">previous</a> |</li>
  132. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  133. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  134. <li class="switchers">
  135. <div class="language_switcher_placeholder"></div>
  136. <div class="version_switcher_placeholder"></div>
  137. </li>
  138. <li>
  139. </li>
  140. <li id="cpython-language-and-version">
  141. <a href="../index.html">3.12.3 Documentation</a> &#187;
  142. </li>
  143. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  144. <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">Data Types</a> &#187;</li>
  145. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code> — Data pretty printer</a></li>
  146. <li class="right">
  147. <div class="inline-search" role="search">
  148. <form class="inline-search" action="../search.html" method="get">
  149. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  150. <input type="submit" value="Go" />
  151. </form>
  152. </div>
  153. |
  154. </li>
  155. <li class="right">
  156. <label class="theme-selector-label">
  157. Theme
  158. <select class="theme-selector" oninput="activateTheme(this.value)">
  159. <option value="auto" selected>Auto</option>
  160. <option value="light">Light</option>
  161. <option value="dark">Dark</option>
  162. </select>
  163. </label> |</li>
  164. </ul>
  165. </div>
  166. <div class="document">
  167. <div class="documentwrapper">
  168. <div class="bodywrapper">
  169. <div class="body" role="main">
  170. <section id="module-pprint">
  171. <span id="pprint-data-pretty-printer"></span><h1><a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code></a> — Data pretty printer<a class="headerlink" href="#module-pprint" title="Link to this heading">¶</a></h1>
  172. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/pprint.py">Lib/pprint.py</a></p>
  173. <hr class="docutils" />
  174. <p>The <a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code></a> module provides a capability to “pretty-print” arbitrary
  175. Python data structures in a form which can be used as input to the interpreter.
  176. If the formatted structures include objects which are not fundamental Python
  177. types, the representation may not be loadable. This may be the case if objects
  178. such as files, sockets or classes are included, as well as many other
  179. objects which are not representable as Python literals.</p>
  180. <p>The formatted representation keeps objects on a single line if it can, and
  181. breaks them onto multiple lines if they don’t fit within the allowed width.
  182. Construct <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> objects explicitly if you need to adjust the
  183. width constraint.</p>
  184. <p>Dictionaries are sorted by key before the display is computed.</p>
  185. <div class="versionchanged">
  186. <p><span class="versionmodified changed">Changed in version 3.9: </span>Added support for pretty-printing <a class="reference internal" href="types.html#types.SimpleNamespace" title="types.SimpleNamespace"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.SimpleNamespace</span></code></a>.</p>
  187. </div>
  188. <div class="versionchanged">
  189. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added support for pretty-printing <a class="reference internal" href="dataclasses.html#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-class docutils literal notranslate"><span class="pre">dataclasses.dataclass</span></code></a>.</p>
  190. </div>
  191. <section id="functions">
  192. <span id="pprint-functions"></span><h2>Functions<a class="headerlink" href="#functions" title="Link to this heading">¶</a></h2>
  193. <dl class="py function">
  194. <dt class="sig sig-object py" id="pprint.pp">
  195. <span class="sig-prename descclassname"><span class="pre">pprint.</span></span><span class="sig-name descname"><span class="pre">pp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sort_dicts</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.pp" title="Link to this definition">¶</a></dt>
  196. <dd><p>Prints the formatted representation of <em>object</em> followed by a newline.
  197. If <em>sort_dicts</em> is false (the default), dictionaries will be displayed with
  198. their keys in insertion order, otherwise the dict keys will be sorted.
  199. <em>args</em> and <em>kwargs</em> will be passed to <a class="reference internal" href="#pprint.pprint" title="pprint.pprint"><code class="xref py py-func docutils literal notranslate"><span class="pre">pprint()</span></code></a> as formatting
  200. parameters.</p>
  201. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pprint</span>
  202. <span class="gp">&gt;&gt;&gt; </span><span class="n">stuff</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="s1">&#39;eggs&#39;</span><span class="p">,</span> <span class="s1">&#39;lumberjack&#39;</span><span class="p">,</span> <span class="s1">&#39;knights&#39;</span><span class="p">,</span> <span class="s1">&#39;ni&#39;</span><span class="p">]</span>
  203. <span class="gp">&gt;&gt;&gt; </span><span class="n">stuff</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">stuff</span><span class="p">)</span>
  204. <span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pp</span><span class="p">(</span><span class="n">stuff</span><span class="p">)</span>
  205. <span class="go">[&lt;Recursion on list with id=...&gt;,</span>
  206. <span class="go"> &#39;spam&#39;,</span>
  207. <span class="go"> &#39;eggs&#39;,</span>
  208. <span class="go"> &#39;lumberjack&#39;,</span>
  209. <span class="go"> &#39;knights&#39;,</span>
  210. <span class="go"> &#39;ni&#39;]</span>
  211. </pre></div>
  212. </div>
  213. <div class="versionadded">
  214. <p><span class="versionmodified added">New in version 3.8.</span></p>
  215. </div>
  216. </dd></dl>
  217. <dl class="py function">
  218. <dt class="sig sig-object py" id="pprint.pprint">
  219. <span class="sig-prename descclassname"><span class="pre">pprint.</span></span><span class="sig-name descname"><span class="pre">pprint</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stream</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">width</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">80</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">depth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compact</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sort_dicts</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">underscore_numbers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.pprint" title="Link to this definition">¶</a></dt>
  220. <dd><p>Prints the formatted representation of <em>object</em> on <em>stream</em>, followed by a
  221. newline. If <em>stream</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a> is used. This may be used
  222. in the interactive interpreter instead of the <a class="reference internal" href="functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> function for
  223. inspecting values (you can even reassign <code class="docutils literal notranslate"><span class="pre">print</span> <span class="pre">=</span> <span class="pre">pprint.pprint</span></code> for use
  224. within a scope).</p>
  225. <p>The configuration parameters <em>stream</em>, <em>indent</em>, <em>width</em>, <em>depth</em>,
  226. <em>compact</em>, <em>sort_dicts</em> and <em>underscore_numbers</em> are passed to the
  227. <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> constructor and their meanings are as
  228. described in its documentation below.</p>
  229. <p>Note that <em>sort_dicts</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> by default and you might want to use
  230. <a class="reference internal" href="#pprint.pp" title="pprint.pp"><code class="xref py py-func docutils literal notranslate"><span class="pre">pp()</span></code></a> instead where it is <code class="docutils literal notranslate"><span class="pre">False</span></code> by default.</p>
  231. </dd></dl>
  232. <dl class="py function">
  233. <dt class="sig sig-object py" id="pprint.pformat">
  234. <span class="sig-prename descclassname"><span class="pre">pprint.</span></span><span class="sig-name descname"><span class="pre">pformat</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">width</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">80</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">depth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compact</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sort_dicts</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">underscore_numbers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.pformat" title="Link to this definition">¶</a></dt>
  235. <dd><p>Return the formatted representation of <em>object</em> as a string. <em>indent</em>,
  236. <em>width</em>, <em>depth</em>, <em>compact</em>, <em>sort_dicts</em> and <em>underscore_numbers</em> are
  237. passed to the <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> constructor as formatting parameters
  238. and their meanings are as described in its documentation below.</p>
  239. </dd></dl>
  240. <dl class="py function">
  241. <dt class="sig sig-object py" id="pprint.isreadable">
  242. <span class="sig-prename descclassname"><span class="pre">pprint.</span></span><span class="sig-name descname"><span class="pre">isreadable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.isreadable" title="Link to this definition">¶</a></dt>
  243. <dd><p id="index-0">Determine if the formatted representation of <em>object</em> is “readable”, or can be
  244. used to reconstruct the value using <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>. This always returns <code class="docutils literal notranslate"><span class="pre">False</span></code>
  245. for recursive objects.</p>
  246. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">isreadable</span><span class="p">(</span><span class="n">stuff</span><span class="p">)</span>
  247. <span class="go">False</span>
  248. </pre></div>
  249. </div>
  250. </dd></dl>
  251. <dl class="py function">
  252. <dt class="sig sig-object py" id="pprint.isrecursive">
  253. <span class="sig-prename descclassname"><span class="pre">pprint.</span></span><span class="sig-name descname"><span class="pre">isrecursive</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.isrecursive" title="Link to this definition">¶</a></dt>
  254. <dd><p>Determine if <em>object</em> requires a recursive representation. This function is
  255. subject to the same limitations as noted in <a class="reference internal" href="#pprint.saferepr" title="pprint.saferepr"><code class="xref py py-func docutils literal notranslate"><span class="pre">saferepr()</span></code></a> below and may raise an
  256. <a class="reference internal" href="exceptions.html#RecursionError" title="RecursionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RecursionError</span></code></a> if it fails to detect a recursive object.</p>
  257. </dd></dl>
  258. <dl class="py function">
  259. <dt class="sig sig-object py" id="pprint.saferepr">
  260. <span class="sig-prename descclassname"><span class="pre">pprint.</span></span><span class="sig-name descname"><span class="pre">saferepr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.saferepr" title="Link to this definition">¶</a></dt>
  261. <dd><p>Return a string representation of <em>object</em>, protected against recursion in
  262. some common data structures, namely instances of <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>, <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a>
  263. and <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> or subclasses whose <code class="docutils literal notranslate"><span class="pre">__repr__</span></code> has not been overridden. If the
  264. representation of object exposes a recursive entry, the recursive reference
  265. will be represented as <code class="docutils literal notranslate"><span class="pre">&lt;Recursion</span> <span class="pre">on</span> <span class="pre">typename</span> <span class="pre">with</span> <span class="pre">id=number&gt;</span></code>. The
  266. representation is not otherwise formatted.</p>
  267. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">saferepr</span><span class="p">(</span><span class="n">stuff</span><span class="p">)</span>
  268. <span class="go">&quot;[&lt;Recursion on list with id=...&gt;, &#39;spam&#39;, &#39;eggs&#39;, &#39;lumberjack&#39;, &#39;knights&#39;, &#39;ni&#39;]&quot;</span>
  269. </pre></div>
  270. </div>
  271. </dd></dl>
  272. </section>
  273. <section id="prettyprinter-objects">
  274. <span id="id1"></span><h2>PrettyPrinter Objects<a class="headerlink" href="#prettyprinter-objects" title="Link to this heading">¶</a></h2>
  275. <p>This module defines one class:</p>
  276. <dl class="py class" id="index-1">
  277. <dt class="sig sig-object py" id="pprint.PrettyPrinter">
  278. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pprint.</span></span><span class="sig-name descname"><span class="pre">PrettyPrinter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">width</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">80</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">depth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stream</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compact</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sort_dicts</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">underscore_numbers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter" title="Link to this definition">¶</a></dt>
  279. <dd><p>Construct a <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> instance. This constructor understands
  280. several keyword parameters.</p>
  281. <p><em>stream</em> (default <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code>) is a <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like object</span></a> to
  282. which the output will be written by calling its <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> method.
  283. If both <em>stream</em> and <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code> are <code class="docutils literal notranslate"><span class="pre">None</span></code>, then
  284. <a class="reference internal" href="#pprint.PrettyPrinter.pprint" title="pprint.PrettyPrinter.pprint"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pprint()</span></code></a> silently returns.</p>
  285. <p>Other values configure the manner in which nesting of complex data
  286. structures is displayed.</p>
  287. <p><em>indent</em> (default 1) specifies the amount of indentation added for
  288. each nesting level.</p>
  289. <p><em>depth</em> controls the number of nesting levels which may be printed; if
  290. the data structure being printed is too deep, the next contained level
  291. is replaced by <code class="docutils literal notranslate"><span class="pre">...</span></code>. By default, there is no constraint on the
  292. depth of the objects being formatted.</p>
  293. <p><em>width</em> (default 80) specifies the desired maximum number of characters per
  294. line in the output. If a structure cannot be formatted within the width
  295. constraint, a best effort will be made.</p>
  296. <p><em>compact</em> impacts the way that long sequences (lists, tuples, sets, etc)
  297. are formatted. If <em>compact</em> is false (the default) then each item of a
  298. sequence will be formatted on a separate line. If <em>compact</em> is true, as
  299. many items as will fit within the <em>width</em> will be formatted on each output
  300. line.</p>
  301. <p>If <em>sort_dicts</em> is true (the default), dictionaries will be formatted with
  302. their keys sorted, otherwise they will display in insertion order.</p>
  303. <p>If <em>underscore_numbers</em> is true, integers will be formatted with the
  304. <code class="docutils literal notranslate"><span class="pre">_</span></code> character for a thousands separator, otherwise underscores are not
  305. displayed (the default).</p>
  306. <div class="versionchanged">
  307. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <em>compact</em> parameter.</p>
  308. </div>
  309. <div class="versionchanged">
  310. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <em>sort_dicts</em> parameter.</p>
  311. </div>
  312. <div class="versionchanged">
  313. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>underscore_numbers</em> parameter.</p>
  314. </div>
  315. <div class="versionchanged">
  316. <p><span class="versionmodified changed">Changed in version 3.11: </span>No longer attempts to write to <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code> if it is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  317. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pprint</span>
  318. <span class="gp">&gt;&gt;&gt; </span><span class="n">stuff</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="s1">&#39;eggs&#39;</span><span class="p">,</span> <span class="s1">&#39;lumberjack&#39;</span><span class="p">,</span> <span class="s1">&#39;knights&#39;</span><span class="p">,</span> <span class="s1">&#39;ni&#39;</span><span class="p">]</span>
  319. <span class="gp">&gt;&gt;&gt; </span><span class="n">stuff</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">stuff</span><span class="p">[:])</span>
  320. <span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span> <span class="o">=</span> <span class="n">pprint</span><span class="o">.</span><span class="n">PrettyPrinter</span><span class="p">(</span><span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
  321. <span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">stuff</span><span class="p">)</span>
  322. <span class="go">[ [&#39;spam&#39;, &#39;eggs&#39;, &#39;lumberjack&#39;, &#39;knights&#39;, &#39;ni&#39;],</span>
  323. <span class="go"> &#39;spam&#39;,</span>
  324. <span class="go"> &#39;eggs&#39;,</span>
  325. <span class="go"> &#39;lumberjack&#39;,</span>
  326. <span class="go"> &#39;knights&#39;,</span>
  327. <span class="go"> &#39;ni&#39;]</span>
  328. <span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span> <span class="o">=</span> <span class="n">pprint</span><span class="o">.</span><span class="n">PrettyPrinter</span><span class="p">(</span><span class="n">width</span><span class="o">=</span><span class="mi">41</span><span class="p">,</span> <span class="n">compact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  329. <span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">stuff</span><span class="p">)</span>
  330. <span class="go">[[&#39;spam&#39;, &#39;eggs&#39;, &#39;lumberjack&#39;,</span>
  331. <span class="go"> &#39;knights&#39;, &#39;ni&#39;],</span>
  332. <span class="go"> &#39;spam&#39;, &#39;eggs&#39;, &#39;lumberjack&#39;, &#39;knights&#39;,</span>
  333. <span class="go"> &#39;ni&#39;]</span>
  334. <span class="gp">&gt;&gt;&gt; </span><span class="n">tup</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;eggs&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;lumberjack&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;knights&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;ni&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;dead&#39;</span><span class="p">,</span>
  335. <span class="gp">... </span><span class="p">(</span><span class="s1">&#39;parrot&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;fresh fruit&#39;</span><span class="p">,))))))))</span>
  336. <span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span> <span class="o">=</span> <span class="n">pprint</span><span class="o">.</span><span class="n">PrettyPrinter</span><span class="p">(</span><span class="n">depth</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
  337. <span class="gp">&gt;&gt;&gt; </span><span class="n">pp</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">tup</span><span class="p">)</span>
  338. <span class="go">(&#39;spam&#39;, (&#39;eggs&#39;, (&#39;lumberjack&#39;, (&#39;knights&#39;, (&#39;ni&#39;, (&#39;dead&#39;, (...)))))))</span>
  339. </pre></div>
  340. </div>
  341. </div>
  342. </dd></dl>
  343. <p><a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> instances have the following methods:</p>
  344. <dl class="py method">
  345. <dt class="sig sig-object py" id="pprint.PrettyPrinter.pformat">
  346. <span class="sig-prename descclassname"><span class="pre">PrettyPrinter.</span></span><span class="sig-name descname"><span class="pre">pformat</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter.pformat" title="Link to this definition">¶</a></dt>
  347. <dd><p>Return the formatted representation of <em>object</em>. This takes into account the
  348. options passed to the <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> constructor.</p>
  349. </dd></dl>
  350. <dl class="py method">
  351. <dt class="sig sig-object py" id="pprint.PrettyPrinter.pprint">
  352. <span class="sig-prename descclassname"><span class="pre">PrettyPrinter.</span></span><span class="sig-name descname"><span class="pre">pprint</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter.pprint" title="Link to this definition">¶</a></dt>
  353. <dd><p>Print the formatted representation of <em>object</em> on the configured stream,
  354. followed by a newline.</p>
  355. </dd></dl>
  356. <p>The following methods provide the implementations for the corresponding
  357. functions of the same names. Using these methods on an instance is slightly
  358. more efficient since new <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> objects don’t need to be
  359. created.</p>
  360. <dl class="py method">
  361. <dt class="sig sig-object py" id="pprint.PrettyPrinter.isreadable">
  362. <span class="sig-prename descclassname"><span class="pre">PrettyPrinter.</span></span><span class="sig-name descname"><span class="pre">isreadable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter.isreadable" title="Link to this definition">¶</a></dt>
  363. <dd><p id="index-2">Determine if the formatted representation of the object is “readable,” or can be
  364. used to reconstruct the value using <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>. Note that this returns
  365. <code class="docutils literal notranslate"><span class="pre">False</span></code> for recursive objects. If the <em>depth</em> parameter of the
  366. <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrettyPrinter</span></code></a> is set and the object is deeper than allowed, this
  367. returns <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  368. </dd></dl>
  369. <dl class="py method">
  370. <dt class="sig sig-object py" id="pprint.PrettyPrinter.isrecursive">
  371. <span class="sig-prename descclassname"><span class="pre">PrettyPrinter.</span></span><span class="sig-name descname"><span class="pre">isrecursive</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter.isrecursive" title="Link to this definition">¶</a></dt>
  372. <dd><p>Determine if the object requires a recursive representation.</p>
  373. </dd></dl>
  374. <p>This method is provided as a hook to allow subclasses to modify the way objects
  375. are converted to strings. The default implementation uses the internals of the
  376. <a class="reference internal" href="#pprint.saferepr" title="pprint.saferepr"><code class="xref py py-func docutils literal notranslate"><span class="pre">saferepr()</span></code></a> implementation.</p>
  377. <dl class="py method">
  378. <dt class="sig sig-object py" id="pprint.PrettyPrinter.format">
  379. <span class="sig-prename descclassname"><span class="pre">PrettyPrinter.</span></span><span class="sig-name descname"><span class="pre">format</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxlevels</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pprint.PrettyPrinter.format" title="Link to this definition">¶</a></dt>
  380. <dd><p>Returns three values: the formatted version of <em>object</em> as a string, a flag
  381. indicating whether the result is readable, and a flag indicating whether
  382. recursion was detected. The first argument is the object to be presented. The
  383. second is a dictionary which contains the <a class="reference internal" href="functions.html#id" title="id"><code class="xref py py-func docutils literal notranslate"><span class="pre">id()</span></code></a> of objects that are part of
  384. the current presentation context (direct and indirect containers for <em>object</em>
  385. that are affecting the presentation) as the keys; if an object needs to be
  386. presented which is already represented in <em>context</em>, the third return value
  387. should be <code class="docutils literal notranslate"><span class="pre">True</span></code>. Recursive calls to the <a class="reference internal" href="#pprint.PrettyPrinter.format" title="pprint.PrettyPrinter.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method should add
  388. additional entries for containers to this dictionary. The third argument,
  389. <em>maxlevels</em>, gives the requested limit to recursion; this will be <code class="docutils literal notranslate"><span class="pre">0</span></code> if there
  390. is no requested limit. This argument should be passed unmodified to recursive
  391. calls. The fourth argument, <em>level</em>, gives the current level; recursive calls
  392. should be passed a value less than that of the current call.</p>
  393. </dd></dl>
  394. </section>
  395. <section id="example">
  396. <span id="pprint-example"></span><h2>Example<a class="headerlink" href="#example" title="Link to this heading">¶</a></h2>
  397. <p>To demonstrate several uses of the <a class="reference internal" href="#pprint.pp" title="pprint.pp"><code class="xref py py-func docutils literal notranslate"><span class="pre">pp()</span></code></a> function and its parameters,
  398. let’s fetch information about a project from <a class="reference external" href="https://pypi.org">PyPI</a>:</p>
  399. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
  400. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pprint</span>
  401. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">urllib.request</span> <span class="kn">import</span> <span class="n">urlopen</span>
  402. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">urlopen</span><span class="p">(</span><span class="s1">&#39;https://pypi.org/pypi/sampleproject/json&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">resp</span><span class="p">:</span>
  403. <span class="gp">... </span> <span class="n">project_info</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">resp</span><span class="p">)[</span><span class="s1">&#39;info&#39;</span><span class="p">]</span>
  404. </pre></div>
  405. </div>
  406. <p>In its basic form, <a class="reference internal" href="#pprint.pp" title="pprint.pp"><code class="xref py py-func docutils literal notranslate"><span class="pre">pp()</span></code></a> shows the whole object:</p>
  407. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pp</span><span class="p">(</span><span class="n">project_info</span><span class="p">)</span>
  408. <span class="go">{&#39;author&#39;: &#39;The Python Packaging Authority&#39;,</span>
  409. <span class="go"> &#39;author_email&#39;: &#39;pypa-dev@googlegroups.com&#39;,</span>
  410. <span class="go"> &#39;bugtrack_url&#39;: None,</span>
  411. <span class="go"> &#39;classifiers&#39;: [&#39;Development Status :: 3 - Alpha&#39;,</span>
  412. <span class="go"> &#39;Intended Audience :: Developers&#39;,</span>
  413. <span class="go"> &#39;License :: OSI Approved :: MIT License&#39;,</span>
  414. <span class="go"> &#39;Programming Language :: Python :: 2&#39;,</span>
  415. <span class="go"> &#39;Programming Language :: Python :: 2.6&#39;,</span>
  416. <span class="go"> &#39;Programming Language :: Python :: 2.7&#39;,</span>
  417. <span class="go"> &#39;Programming Language :: Python :: 3&#39;,</span>
  418. <span class="go"> &#39;Programming Language :: Python :: 3.2&#39;,</span>
  419. <span class="go"> &#39;Programming Language :: Python :: 3.3&#39;,</span>
  420. <span class="go"> &#39;Programming Language :: Python :: 3.4&#39;,</span>
  421. <span class="go"> &#39;Topic :: Software Development :: Build Tools&#39;],</span>
  422. <span class="go"> &#39;description&#39;: &#39;A sample Python project\n&#39;</span>
  423. <span class="go"> &#39;=======================\n&#39;</span>
  424. <span class="go"> &#39;\n&#39;</span>
  425. <span class="go"> &#39;This is the description file for the project.\n&#39;</span>
  426. <span class="go"> &#39;\n&#39;</span>
  427. <span class="go"> &#39;The file should use UTF-8 encoding and be written using &#39;</span>
  428. <span class="go"> &#39;ReStructured Text. It\n&#39;</span>
  429. <span class="go"> &#39;will be used to generate the project webpage on PyPI, and &#39;</span>
  430. <span class="go"> &#39;should be written for\n&#39;</span>
  431. <span class="go"> &#39;that purpose.\n&#39;</span>
  432. <span class="go"> &#39;\n&#39;</span>
  433. <span class="go"> &#39;Typical contents for this file would include an overview of &#39;</span>
  434. <span class="go"> &#39;the project, basic\n&#39;</span>
  435. <span class="go"> &#39;usage examples, etc. Generally, including the project &#39;</span>
  436. <span class="go"> &#39;changelog in here is not\n&#39;</span>
  437. <span class="go"> &#39;a good idea, although a simple &quot;What\&#39;s New&quot; section for the &#39;</span>
  438. <span class="go"> &#39;most recent version\n&#39;</span>
  439. <span class="go"> &#39;may be appropriate.&#39;,</span>
  440. <span class="go"> &#39;description_content_type&#39;: None,</span>
  441. <span class="go"> &#39;docs_url&#39;: None,</span>
  442. <span class="go"> &#39;download_url&#39;: &#39;UNKNOWN&#39;,</span>
  443. <span class="go"> &#39;downloads&#39;: {&#39;last_day&#39;: -1, &#39;last_month&#39;: -1, &#39;last_week&#39;: -1},</span>
  444. <span class="go"> &#39;home_page&#39;: &#39;https://github.com/pypa/sampleproject&#39;,</span>
  445. <span class="go"> &#39;keywords&#39;: &#39;sample setuptools development&#39;,</span>
  446. <span class="go"> &#39;license&#39;: &#39;MIT&#39;,</span>
  447. <span class="go"> &#39;maintainer&#39;: None,</span>
  448. <span class="go"> &#39;maintainer_email&#39;: None,</span>
  449. <span class="go"> &#39;name&#39;: &#39;sampleproject&#39;,</span>
  450. <span class="go"> &#39;package_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
  451. <span class="go"> &#39;platform&#39;: &#39;UNKNOWN&#39;,</span>
  452. <span class="go"> &#39;project_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
  453. <span class="go"> &#39;project_urls&#39;: {&#39;Download&#39;: &#39;UNKNOWN&#39;,</span>
  454. <span class="go"> &#39;Homepage&#39;: &#39;https://github.com/pypa/sampleproject&#39;},</span>
  455. <span class="go"> &#39;release_url&#39;: &#39;https://pypi.org/project/sampleproject/1.2.0/&#39;,</span>
  456. <span class="go"> &#39;requires_dist&#39;: None,</span>
  457. <span class="go"> &#39;requires_python&#39;: None,</span>
  458. <span class="go"> &#39;summary&#39;: &#39;A sample Python project&#39;,</span>
  459. <span class="go"> &#39;version&#39;: &#39;1.2.0&#39;}</span>
  460. </pre></div>
  461. </div>
  462. <p>The result can be limited to a certain <em>depth</em> (ellipsis is used for deeper
  463. contents):</p>
  464. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pp</span><span class="p">(</span><span class="n">project_info</span><span class="p">,</span> <span class="n">depth</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
  465. <span class="go">{&#39;author&#39;: &#39;The Python Packaging Authority&#39;,</span>
  466. <span class="go"> &#39;author_email&#39;: &#39;pypa-dev@googlegroups.com&#39;,</span>
  467. <span class="go"> &#39;bugtrack_url&#39;: None,</span>
  468. <span class="go"> &#39;classifiers&#39;: [...],</span>
  469. <span class="go"> &#39;description&#39;: &#39;A sample Python project\n&#39;</span>
  470. <span class="go"> &#39;=======================\n&#39;</span>
  471. <span class="go"> &#39;\n&#39;</span>
  472. <span class="go"> &#39;This is the description file for the project.\n&#39;</span>
  473. <span class="go"> &#39;\n&#39;</span>
  474. <span class="go"> &#39;The file should use UTF-8 encoding and be written using &#39;</span>
  475. <span class="go"> &#39;ReStructured Text. It\n&#39;</span>
  476. <span class="go"> &#39;will be used to generate the project webpage on PyPI, and &#39;</span>
  477. <span class="go"> &#39;should be written for\n&#39;</span>
  478. <span class="go"> &#39;that purpose.\n&#39;</span>
  479. <span class="go"> &#39;\n&#39;</span>
  480. <span class="go"> &#39;Typical contents for this file would include an overview of &#39;</span>
  481. <span class="go"> &#39;the project, basic\n&#39;</span>
  482. <span class="go"> &#39;usage examples, etc. Generally, including the project &#39;</span>
  483. <span class="go"> &#39;changelog in here is not\n&#39;</span>
  484. <span class="go"> &#39;a good idea, although a simple &quot;What\&#39;s New&quot; section for the &#39;</span>
  485. <span class="go"> &#39;most recent version\n&#39;</span>
  486. <span class="go"> &#39;may be appropriate.&#39;,</span>
  487. <span class="go"> &#39;description_content_type&#39;: None,</span>
  488. <span class="go"> &#39;docs_url&#39;: None,</span>
  489. <span class="go"> &#39;download_url&#39;: &#39;UNKNOWN&#39;,</span>
  490. <span class="go"> &#39;downloads&#39;: {...},</span>
  491. <span class="go"> &#39;home_page&#39;: &#39;https://github.com/pypa/sampleproject&#39;,</span>
  492. <span class="go"> &#39;keywords&#39;: &#39;sample setuptools development&#39;,</span>
  493. <span class="go"> &#39;license&#39;: &#39;MIT&#39;,</span>
  494. <span class="go"> &#39;maintainer&#39;: None,</span>
  495. <span class="go"> &#39;maintainer_email&#39;: None,</span>
  496. <span class="go"> &#39;name&#39;: &#39;sampleproject&#39;,</span>
  497. <span class="go"> &#39;package_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
  498. <span class="go"> &#39;platform&#39;: &#39;UNKNOWN&#39;,</span>
  499. <span class="go"> &#39;project_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
  500. <span class="go"> &#39;project_urls&#39;: {...},</span>
  501. <span class="go"> &#39;release_url&#39;: &#39;https://pypi.org/project/sampleproject/1.2.0/&#39;,</span>
  502. <span class="go"> &#39;requires_dist&#39;: None,</span>
  503. <span class="go"> &#39;requires_python&#39;: None,</span>
  504. <span class="go"> &#39;summary&#39;: &#39;A sample Python project&#39;,</span>
  505. <span class="go"> &#39;version&#39;: &#39;1.2.0&#39;}</span>
  506. </pre></div>
  507. </div>
  508. <p>Additionally, maximum character <em>width</em> can be suggested. If a long object
  509. cannot be split, the specified width will be exceeded:</p>
  510. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pp</span><span class="p">(</span><span class="n">project_info</span><span class="p">,</span> <span class="n">depth</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span>
  511. <span class="go">{&#39;author&#39;: &#39;The Python Packaging Authority&#39;,</span>
  512. <span class="go"> &#39;author_email&#39;: &#39;pypa-dev@googlegroups.com&#39;,</span>
  513. <span class="go"> &#39;bugtrack_url&#39;: None,</span>
  514. <span class="go"> &#39;classifiers&#39;: [...],</span>
  515. <span class="go"> &#39;description&#39;: &#39;A sample Python project\n&#39;</span>
  516. <span class="go"> &#39;=======================\n&#39;</span>
  517. <span class="go"> &#39;\n&#39;</span>
  518. <span class="go"> &#39;This is the description file for the &#39;</span>
  519. <span class="go"> &#39;project.\n&#39;</span>
  520. <span class="go"> &#39;\n&#39;</span>
  521. <span class="go"> &#39;The file should use UTF-8 encoding and be &#39;</span>
  522. <span class="go"> &#39;written using ReStructured Text. It\n&#39;</span>
  523. <span class="go"> &#39;will be used to generate the project &#39;</span>
  524. <span class="go"> &#39;webpage on PyPI, and should be written &#39;</span>
  525. <span class="go"> &#39;for\n&#39;</span>
  526. <span class="go"> &#39;that purpose.\n&#39;</span>
  527. <span class="go"> &#39;\n&#39;</span>
  528. <span class="go"> &#39;Typical contents for this file would &#39;</span>
  529. <span class="go"> &#39;include an overview of the project, &#39;</span>
  530. <span class="go"> &#39;basic\n&#39;</span>
  531. <span class="go"> &#39;usage examples, etc. Generally, including &#39;</span>
  532. <span class="go"> &#39;the project changelog in here is not\n&#39;</span>
  533. <span class="go"> &#39;a good idea, although a simple &quot;What\&#39;s &#39;</span>
  534. <span class="go"> &#39;New&quot; section for the most recent version\n&#39;</span>
  535. <span class="go"> &#39;may be appropriate.&#39;,</span>
  536. <span class="go"> &#39;description_content_type&#39;: None,</span>
  537. <span class="go"> &#39;docs_url&#39;: None,</span>
  538. <span class="go"> &#39;download_url&#39;: &#39;UNKNOWN&#39;,</span>
  539. <span class="go"> &#39;downloads&#39;: {...},</span>
  540. <span class="go"> &#39;home_page&#39;: &#39;https://github.com/pypa/sampleproject&#39;,</span>
  541. <span class="go"> &#39;keywords&#39;: &#39;sample setuptools development&#39;,</span>
  542. <span class="go"> &#39;license&#39;: &#39;MIT&#39;,</span>
  543. <span class="go"> &#39;maintainer&#39;: None,</span>
  544. <span class="go"> &#39;maintainer_email&#39;: None,</span>
  545. <span class="go"> &#39;name&#39;: &#39;sampleproject&#39;,</span>
  546. <span class="go"> &#39;package_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
  547. <span class="go"> &#39;platform&#39;: &#39;UNKNOWN&#39;,</span>
  548. <span class="go"> &#39;project_url&#39;: &#39;https://pypi.org/project/sampleproject/&#39;,</span>
  549. <span class="go"> &#39;project_urls&#39;: {...},</span>
  550. <span class="go"> &#39;release_url&#39;: &#39;https://pypi.org/project/sampleproject/1.2.0/&#39;,</span>
  551. <span class="go"> &#39;requires_dist&#39;: None,</span>
  552. <span class="go"> &#39;requires_python&#39;: None,</span>
  553. <span class="go"> &#39;summary&#39;: &#39;A sample Python project&#39;,</span>
  554. <span class="go"> &#39;version&#39;: &#39;1.2.0&#39;}</span>
  555. </pre></div>
  556. </div>
  557. </section>
  558. </section>
  559. <div class="clearer"></div>
  560. </div>
  561. </div>
  562. </div>
  563. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  564. <div class="sphinxsidebarwrapper">
  565. <div>
  566. <h3><a href="../contents.html">Table of Contents</a></h3>
  567. <ul>
  568. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code> — Data pretty printer</a><ul>
  569. <li><a class="reference internal" href="#functions">Functions</a></li>
  570. <li><a class="reference internal" href="#prettyprinter-objects">PrettyPrinter Objects</a></li>
  571. <li><a class="reference internal" href="#example">Example</a></li>
  572. </ul>
  573. </li>
  574. </ul>
  575. </div>
  576. <div>
  577. <h4>Previous topic</h4>
  578. <p class="topless"><a href="copy.html"
  579. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copy</span></code> — Shallow and deep copy operations</a></p>
  580. </div>
  581. <div>
  582. <h4>Next topic</h4>
  583. <p class="topless"><a href="reprlib.html"
  584. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> — Alternate <code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code> implementation</a></p>
  585. </div>
  586. <div role="note" aria-label="source link">
  587. <h3>This Page</h3>
  588. <ul class="this-page-menu">
  589. <li><a href="../bugs.html">Report a Bug</a></li>
  590. <li>
  591. <a href="https://github.com/python/cpython/blob/main/Doc/library/pprint.rst"
  592. rel="nofollow">Show Source
  593. </a>
  594. </li>
  595. </ul>
  596. </div>
  597. </div>
  598. <div id="sidebarbutton" title="Collapse sidebar">
  599. <span>«</span>
  600. </div>
  601. </div>
  602. <div class="clearer"></div>
  603. </div>
  604. <div class="related" role="navigation" aria-label="related navigation">
  605. <h3>Navigation</h3>
  606. <ul>
  607. <li class="right" style="margin-right: 10px">
  608. <a href="../genindex.html" title="General Index"
  609. >index</a></li>
  610. <li class="right" >
  611. <a href="../py-modindex.html" title="Python Module Index"
  612. >modules</a> |</li>
  613. <li class="right" >
  614. <a href="reprlib.html" title="reprlib — Alternate repr() implementation"
  615. >next</a> |</li>
  616. <li class="right" >
  617. <a href="copy.html" title="copy — Shallow and deep copy operations"
  618. >previous</a> |</li>
  619. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  620. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  621. <li class="switchers">
  622. <div class="language_switcher_placeholder"></div>
  623. <div class="version_switcher_placeholder"></div>
  624. </li>
  625. <li>
  626. </li>
  627. <li id="cpython-language-and-version">
  628. <a href="../index.html">3.12.3 Documentation</a> &#187;
  629. </li>
  630. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  631. <li class="nav-item nav-item-2"><a href="datatypes.html" >Data Types</a> &#187;</li>
  632. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code> — Data pretty printer</a></li>
  633. <li class="right">
  634. <div class="inline-search" role="search">
  635. <form class="inline-search" action="../search.html" method="get">
  636. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  637. <input type="submit" value="Go" />
  638. </form>
  639. </div>
  640. |
  641. </li>
  642. <li class="right">
  643. <label class="theme-selector-label">
  644. Theme
  645. <select class="theme-selector" oninput="activateTheme(this.value)">
  646. <option value="auto" selected>Auto</option>
  647. <option value="light">Light</option>
  648. <option value="dark">Dark</option>
  649. </select>
  650. </label> |</li>
  651. </ul>
  652. </div>
  653. <div class="footer">
  654. &copy;
  655. <a href="../copyright.html">
  656. Copyright
  657. </a>
  658. 2001-2024, Python Software Foundation.
  659. <br />
  660. This page is licensed under the Python Software Foundation License Version 2.
  661. <br />
  662. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  663. <br />
  664. See <a href="/license.html">History and License</a> for more information.<br />
  665. <br />
  666. The Python Software Foundation is a non-profit corporation.
  667. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  668. <br />
  669. <br />
  670. Last updated on Apr 09, 2024 (13:47 UTC).
  671. <a href="/bugs.html">Found a bug</a>?
  672. <br />
  673. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  674. </div>
  675. </body>
  676. </html>
上海开阖软件有限公司 沪ICP备12045867号-1