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.

2431 lines
277KB

  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="Built-in Functions" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/functions.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.,,,, Built-in Functions,,, A, abs(), aiter(), all(), a..." />
  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="The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.,,,, Built-in Functions,,, A, abs(), aiter(), all(), a..." />
  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>Built-in Functions &#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="Built-in Constants" href="constants.html" />
  33. <link rel="prev" title="Introduction" href="intro.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/functions.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. <h4>Previous topic</h4>
  84. <p class="topless"><a href="intro.html"
  85. title="previous chapter">Introduction</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="constants.html"
  90. title="next chapter">Built-in Constants</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/functions.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="constants.html" title="Built-in Constants"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="intro.html" title="Introduction"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-this"><a href="">Built-in Functions</a></li>
  134. <li class="right">
  135. <div class="inline-search" role="search">
  136. <form class="inline-search" action="../search.html" method="get">
  137. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  138. <input type="submit" value="Go" />
  139. </form>
  140. </div>
  141. |
  142. </li>
  143. <li class="right">
  144. <label class="theme-selector-label">
  145. Theme
  146. <select class="theme-selector" oninput="activateTheme(this.value)">
  147. <option value="auto" selected>Auto</option>
  148. <option value="light">Light</option>
  149. <option value="dark">Dark</option>
  150. </select>
  151. </label> |</li>
  152. </ul>
  153. </div>
  154. <div class="document">
  155. <div class="documentwrapper">
  156. <div class="bodywrapper">
  157. <div class="body" role="main">
  158. <section id="built-in-functions">
  159. <span id="built-in-funcs"></span><h1>Built-in Functions<a class="headerlink" href="#built-in-functions" title="Link to this heading">¶</a></h1>
  160. <p>The Python interpreter has a number of functions and types built into it that
  161. are always available. They are listed here in alphabetical order.</p>
  162. <table class="docutils align-default">
  163. <thead>
  164. <tr class="row-odd"><th class="head" colspan="4"><p>Built-in Functions</p></th>
  165. </tr>
  166. </thead>
  167. <tbody>
  168. <tr class="row-even"><td><div class="line-block">
  169. <div class="line"><strong>A</strong></div>
  170. <div class="line"><a class="reference internal" href="#abs" title="abs"><code class="xref py py-func docutils literal notranslate"><span class="pre">abs()</span></code></a></div>
  171. <div class="line"><a class="reference internal" href="#aiter" title="aiter"><code class="xref py py-func docutils literal notranslate"><span class="pre">aiter()</span></code></a></div>
  172. <div class="line"><a class="reference internal" href="#all" title="all"><code class="xref py py-func docutils literal notranslate"><span class="pre">all()</span></code></a></div>
  173. <div class="line"><a class="reference internal" href="#anext" title="anext"><code class="xref py py-func docutils literal notranslate"><span class="pre">anext()</span></code></a></div>
  174. <div class="line"><a class="reference internal" href="#any" title="any"><code class="xref py py-func docutils literal notranslate"><span class="pre">any()</span></code></a></div>
  175. <div class="line"><a class="reference internal" href="#ascii" title="ascii"><code class="xref py py-func docutils literal notranslate"><span class="pre">ascii()</span></code></a></div>
  176. <div class="line"><br /></div>
  177. <div class="line"><strong>B</strong></div>
  178. <div class="line"><a class="reference internal" href="#bin" title="bin"><code class="xref py py-func docutils literal notranslate"><span class="pre">bin()</span></code></a></div>
  179. <div class="line"><a class="reference internal" href="#bool" title="bool"><code class="xref py py-func docutils literal notranslate"><span class="pre">bool()</span></code></a></div>
  180. <div class="line"><a class="reference internal" href="#breakpoint" title="breakpoint"><code class="xref py py-func docutils literal notranslate"><span class="pre">breakpoint()</span></code></a></div>
  181. <div class="line"><a class="reference internal" href="#func-bytearray"><code class="docutils literal notranslate"><span class="pre">bytearray()</span></code></a></div>
  182. <div class="line"><a class="reference internal" href="#func-bytes"><code class="docutils literal notranslate"><span class="pre">bytes()</span></code></a></div>
  183. <div class="line"><br /></div>
  184. <div class="line"><strong>C</strong></div>
  185. <div class="line"><a class="reference internal" href="#callable" title="callable"><code class="xref py py-func docutils literal notranslate"><span class="pre">callable()</span></code></a></div>
  186. <div class="line"><a class="reference internal" href="#chr" title="chr"><code class="xref py py-func docutils literal notranslate"><span class="pre">chr()</span></code></a></div>
  187. <div class="line"><a class="reference internal" href="#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a></div>
  188. <div class="line"><a class="reference internal" href="#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a></div>
  189. <div class="line"><a class="reference internal" href="#complex" title="complex"><code class="xref py py-func docutils literal notranslate"><span class="pre">complex()</span></code></a></div>
  190. <div class="line"><br /></div>
  191. <div class="line"><strong>D</strong></div>
  192. <div class="line"><a class="reference internal" href="#delattr" title="delattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">delattr()</span></code></a></div>
  193. <div class="line"><a class="reference internal" href="#func-dict"><code class="docutils literal notranslate"><span class="pre">dict()</span></code></a></div>
  194. <div class="line"><a class="reference internal" href="#dir" title="dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code></a></div>
  195. <div class="line"><a class="reference internal" href="#divmod" title="divmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">divmod()</span></code></a></div>
  196. <div class="line"><br /></div>
  197. </div>
  198. </td>
  199. <td><div class="line-block">
  200. <div class="line"><strong>E</strong></div>
  201. <div class="line"><a class="reference internal" href="#enumerate" title="enumerate"><code class="xref py py-func docutils literal notranslate"><span class="pre">enumerate()</span></code></a></div>
  202. <div class="line"><a class="reference internal" href="#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a></div>
  203. <div class="line"><a class="reference internal" href="#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a></div>
  204. <div class="line"><br /></div>
  205. <div class="line"><strong>F</strong></div>
  206. <div class="line"><a class="reference internal" href="#filter" title="filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">filter()</span></code></a></div>
  207. <div class="line"><a class="reference internal" href="#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a></div>
  208. <div class="line"><a class="reference internal" href="#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a></div>
  209. <div class="line"><a class="reference internal" href="#func-frozenset"><code class="docutils literal notranslate"><span class="pre">frozenset()</span></code></a></div>
  210. <div class="line"><br /></div>
  211. <div class="line"><strong>G</strong></div>
  212. <div class="line"><a class="reference internal" href="#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a></div>
  213. <div class="line"><a class="reference internal" href="#globals" title="globals"><code class="xref py py-func docutils literal notranslate"><span class="pre">globals()</span></code></a></div>
  214. <div class="line"><br /></div>
  215. <div class="line"><strong>H</strong></div>
  216. <div class="line"><a class="reference internal" href="#hasattr" title="hasattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">hasattr()</span></code></a></div>
  217. <div class="line"><a class="reference internal" href="#hash" title="hash"><code class="xref py py-func docutils literal notranslate"><span class="pre">hash()</span></code></a></div>
  218. <div class="line"><a class="reference internal" href="#help" title="help"><code class="xref py py-func docutils literal notranslate"><span class="pre">help()</span></code></a></div>
  219. <div class="line"><a class="reference internal" href="#hex" title="hex"><code class="xref py py-func docutils literal notranslate"><span class="pre">hex()</span></code></a></div>
  220. <div class="line"><br /></div>
  221. <div class="line"><strong>I</strong></div>
  222. <div class="line"><a class="reference internal" href="#id" title="id"><code class="xref py py-func docutils literal notranslate"><span class="pre">id()</span></code></a></div>
  223. <div class="line"><a class="reference internal" href="#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a></div>
  224. <div class="line"><a class="reference internal" href="#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a></div>
  225. <div class="line"><a class="reference internal" href="#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a></div>
  226. <div class="line"><a class="reference internal" href="#issubclass" title="issubclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubclass()</span></code></a></div>
  227. <div class="line"><a class="reference internal" href="#iter" title="iter"><code class="xref py py-func docutils literal notranslate"><span class="pre">iter()</span></code></a></div>
  228. </div>
  229. </td>
  230. <td><div class="line-block">
  231. <div class="line"><strong>L</strong></div>
  232. <div class="line"><a class="reference internal" href="#len" title="len"><code class="xref py py-func docutils literal notranslate"><span class="pre">len()</span></code></a></div>
  233. <div class="line"><a class="reference internal" href="#func-list"><code class="docutils literal notranslate"><span class="pre">list()</span></code></a></div>
  234. <div class="line"><a class="reference internal" href="#locals" title="locals"><code class="xref py py-func docutils literal notranslate"><span class="pre">locals()</span></code></a></div>
  235. <div class="line"><br /></div>
  236. <div class="line"><strong>M</strong></div>
  237. <div class="line"><a class="reference internal" href="#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a></div>
  238. <div class="line"><a class="reference internal" href="#max" title="max"><code class="xref py py-func docutils literal notranslate"><span class="pre">max()</span></code></a></div>
  239. <div class="line"><a class="reference internal" href="#func-memoryview"><code class="docutils literal notranslate"><span class="pre">memoryview()</span></code></a></div>
  240. <div class="line"><a class="reference internal" href="#min" title="min"><code class="xref py py-func docutils literal notranslate"><span class="pre">min()</span></code></a></div>
  241. <div class="line"><br /></div>
  242. <div class="line"><strong>N</strong></div>
  243. <div class="line"><a class="reference internal" href="#next" title="next"><code class="xref py py-func docutils literal notranslate"><span class="pre">next()</span></code></a></div>
  244. <div class="line"><br /></div>
  245. <div class="line"><strong>O</strong></div>
  246. <div class="line"><a class="reference internal" href="#object" title="object"><code class="xref py py-func docutils literal notranslate"><span class="pre">object()</span></code></a></div>
  247. <div class="line"><a class="reference internal" href="#oct" title="oct"><code class="xref py py-func docutils literal notranslate"><span class="pre">oct()</span></code></a></div>
  248. <div class="line"><a class="reference internal" href="#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a></div>
  249. <div class="line"><a class="reference internal" href="#ord" title="ord"><code class="xref py py-func docutils literal notranslate"><span class="pre">ord()</span></code></a></div>
  250. <div class="line"><br /></div>
  251. <div class="line"><strong>P</strong></div>
  252. <div class="line"><a class="reference internal" href="#pow" title="pow"><code class="xref py py-func docutils literal notranslate"><span class="pre">pow()</span></code></a></div>
  253. <div class="line"><a class="reference internal" href="#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a></div>
  254. <div class="line"><a class="reference internal" href="#property" title="property"><code class="xref py py-func docutils literal notranslate"><span class="pre">property()</span></code></a></div>
  255. <div class="line"><br /></div>
  256. <div class="line"><br /></div>
  257. <div class="line"><br /></div>
  258. <div class="line"><br /></div>
  259. </div>
  260. </td>
  261. <td><div class="line-block">
  262. <div class="line"><strong>R</strong></div>
  263. <div class="line"><a class="reference internal" href="#func-range"><code class="docutils literal notranslate"><span class="pre">range()</span></code></a></div>
  264. <div class="line"><a class="reference internal" href="#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a></div>
  265. <div class="line"><a class="reference internal" href="#reversed" title="reversed"><code class="xref py py-func docutils literal notranslate"><span class="pre">reversed()</span></code></a></div>
  266. <div class="line"><a class="reference internal" href="#round" title="round"><code class="xref py py-func docutils literal notranslate"><span class="pre">round()</span></code></a></div>
  267. <div class="line"><br /></div>
  268. <div class="line"><strong>S</strong></div>
  269. <div class="line"><a class="reference internal" href="#func-set"><code class="docutils literal notranslate"><span class="pre">set()</span></code></a></div>
  270. <div class="line"><a class="reference internal" href="#setattr" title="setattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">setattr()</span></code></a></div>
  271. <div class="line"><a class="reference internal" href="#slice" title="slice"><code class="xref py py-func docutils literal notranslate"><span class="pre">slice()</span></code></a></div>
  272. <div class="line"><a class="reference internal" href="#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a></div>
  273. <div class="line"><a class="reference internal" href="#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a></div>
  274. <div class="line"><a class="reference internal" href="#func-str"><code class="docutils literal notranslate"><span class="pre">str()</span></code></a></div>
  275. <div class="line"><a class="reference internal" href="#sum" title="sum"><code class="xref py py-func docutils literal notranslate"><span class="pre">sum()</span></code></a></div>
  276. <div class="line"><a class="reference internal" href="#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a></div>
  277. <div class="line"><br /></div>
  278. <div class="line"><strong>T</strong></div>
  279. <div class="line"><a class="reference internal" href="#func-tuple"><code class="docutils literal notranslate"><span class="pre">tuple()</span></code></a></div>
  280. <div class="line"><a class="reference internal" href="#type" title="type"><code class="xref py py-func docutils literal notranslate"><span class="pre">type()</span></code></a></div>
  281. <div class="line"><br /></div>
  282. <div class="line"><strong>V</strong></div>
  283. <div class="line"><a class="reference internal" href="#vars" title="vars"><code class="xref py py-func docutils literal notranslate"><span class="pre">vars()</span></code></a></div>
  284. <div class="line"><br /></div>
  285. <div class="line"><strong>Z</strong></div>
  286. <div class="line"><a class="reference internal" href="#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a></div>
  287. <div class="line"><br /></div>
  288. <div class="line"><strong>_</strong></div>
  289. <div class="line"><a class="reference internal" href="#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a></div>
  290. </div>
  291. </td>
  292. </tr>
  293. </tbody>
  294. </table>
  295. <dl class="py function">
  296. <dt class="sig sig-object py" id="abs">
  297. <span class="sig-name descname"><span class="pre">abs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#abs" title="Link to this definition">¶</a></dt>
  298. <dd><p>Return the absolute value of a number. The argument may be an
  299. integer, a floating point number, or an object implementing
  300. <a class="reference internal" href="../reference/datamodel.html#object.__abs__" title="object.__abs__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__abs__()</span></code></a>.
  301. If the argument is a complex number, its magnitude is returned.</p>
  302. </dd></dl>
  303. <dl class="py function">
  304. <dt class="sig sig-object py" id="aiter">
  305. <span class="sig-name descname"><span class="pre">aiter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">async_iterable</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#aiter" title="Link to this definition">¶</a></dt>
  306. <dd><p>Return an <a class="reference internal" href="../glossary.html#term-asynchronous-iterator"><span class="xref std std-term">asynchronous iterator</span></a> for an <a class="reference internal" href="../glossary.html#term-asynchronous-iterable"><span class="xref std std-term">asynchronous iterable</span></a>.
  307. Equivalent to calling <code class="docutils literal notranslate"><span class="pre">x.__aiter__()</span></code>.</p>
  308. <p>Note: Unlike <a class="reference internal" href="#iter" title="iter"><code class="xref py py-func docutils literal notranslate"><span class="pre">iter()</span></code></a>, <a class="reference internal" href="#aiter" title="aiter"><code class="xref py py-func docutils literal notranslate"><span class="pre">aiter()</span></code></a> has no 2-argument variant.</p>
  309. <div class="versionadded">
  310. <p><span class="versionmodified added">New in version 3.10.</span></p>
  311. </div>
  312. </dd></dl>
  313. <dl class="py function">
  314. <dt class="sig sig-object py" id="all">
  315. <span class="sig-name descname"><span class="pre">all</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#all" title="Link to this definition">¶</a></dt>
  316. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if all elements of the <em>iterable</em> are true (or if the iterable
  317. is empty). Equivalent to:</p>
  318. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">all</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span>
  319. <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
  320. <span class="k">if</span> <span class="ow">not</span> <span class="n">element</span><span class="p">:</span>
  321. <span class="k">return</span> <span class="kc">False</span>
  322. <span class="k">return</span> <span class="kc">True</span>
  323. </pre></div>
  324. </div>
  325. </dd></dl>
  326. <dl class="py function">
  327. <dt class="sig sig-object py" id="anext">
  328. <em class="property"><span class="pre">awaitable</span> </em><span class="sig-name descname"><span class="pre">anext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">async_iterator</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#anext" title="Link to this definition">¶</a></dt>
  329. <dt class="sig sig-object py">
  330. <em class="property"><span class="pre">awaitable</span> </em><span class="sig-name descname"><span class="pre">anext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">async_iterator</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span></em><span class="sig-paren">)</span></dt>
  331. <dd><p>When awaited, return the next item from the given <a class="reference internal" href="../glossary.html#term-asynchronous-iterator"><span class="xref std std-term">asynchronous
  332. iterator</span></a>, or <em>default</em> if given and the iterator is exhausted.</p>
  333. <p>This is the async variant of the <a class="reference internal" href="#next" title="next"><code class="xref py py-func docutils literal notranslate"><span class="pre">next()</span></code></a> builtin, and behaves
  334. similarly.</p>
  335. <p>This calls the <a class="reference internal" href="../reference/datamodel.html#object.__anext__" title="object.__anext__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__anext__()</span></code></a> method of <em>async_iterator</em>,
  336. returning an <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a>. Awaiting this returns the next value of the
  337. iterator. If <em>default</em> is given, it is returned if the iterator is exhausted,
  338. otherwise <a class="reference internal" href="exceptions.html#StopAsyncIteration" title="StopAsyncIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopAsyncIteration</span></code></a> is raised.</p>
  339. <div class="versionadded">
  340. <p><span class="versionmodified added">New in version 3.10.</span></p>
  341. </div>
  342. </dd></dl>
  343. <dl class="py function">
  344. <dt class="sig sig-object py" id="any">
  345. <span class="sig-name descname"><span class="pre">any</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#any" title="Link to this definition">¶</a></dt>
  346. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if any element of the <em>iterable</em> is true. If the iterable
  347. is empty, return <code class="docutils literal notranslate"><span class="pre">False</span></code>. Equivalent to:</p>
  348. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">any</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span>
  349. <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
  350. <span class="k">if</span> <span class="n">element</span><span class="p">:</span>
  351. <span class="k">return</span> <span class="kc">True</span>
  352. <span class="k">return</span> <span class="kc">False</span>
  353. </pre></div>
  354. </div>
  355. </dd></dl>
  356. <dl class="py function">
  357. <dt class="sig sig-object py" id="ascii">
  358. <span class="sig-name descname"><span class="pre">ascii</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="#ascii" title="Link to this definition">¶</a></dt>
  359. <dd><p>As <a class="reference internal" href="#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a>, return a string containing a printable representation of an
  360. object, but escape the non-ASCII characters in the string returned by
  361. <a class="reference internal" href="#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> using <code class="docutils literal notranslate"><span class="pre">\x</span></code>, <code class="docutils literal notranslate"><span class="pre">\u</span></code>, or <code class="docutils literal notranslate"><span class="pre">\U</span></code> escapes. This generates a string
  362. similar to that returned by <a class="reference internal" href="#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> in Python 2.</p>
  363. </dd></dl>
  364. <dl class="py function">
  365. <dt class="sig sig-object py" id="bin">
  366. <span class="sig-name descname"><span class="pre">bin</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#bin" title="Link to this definition">¶</a></dt>
  367. <dd><p>Convert an integer number to a binary string prefixed with “0b”. The result
  368. is a valid Python expression. If <em>x</em> is not a Python <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> object, it
  369. has to define an <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> method that returns an integer. Some
  370. examples:</p>
  371. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">bin</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
  372. <span class="go">&#39;0b11&#39;</span>
  373. <span class="gp">&gt;&gt;&gt; </span><span class="nb">bin</span><span class="p">(</span><span class="o">-</span><span class="mi">10</span><span class="p">)</span>
  374. <span class="go">&#39;-0b1010&#39;</span>
  375. </pre></div>
  376. </div>
  377. <p>If the prefix “0b” is desired or not, you can use either of the following ways.</p>
  378. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="mi">14</span><span class="p">,</span> <span class="s1">&#39;#b&#39;</span><span class="p">),</span> <span class="nb">format</span><span class="p">(</span><span class="mi">14</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">)</span>
  379. <span class="go">(&#39;0b1110&#39;, &#39;1110&#39;)</span>
  380. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="mi">14</span><span class="si">:</span><span class="s1">#b</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="mi">14</span><span class="si">:</span><span class="s1">b</span><span class="si">}</span><span class="s1">&#39;</span>
  381. <span class="go">(&#39;0b1110&#39;, &#39;1110&#39;)</span>
  382. </pre></div>
  383. </div>
  384. <p>See also <a class="reference internal" href="#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> for more information.</p>
  385. </dd></dl>
  386. <dl class="py class">
  387. <dt class="sig sig-object py" id="bool">
  388. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">bool</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</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="#bool" title="Link to this definition">¶</a></dt>
  389. <dd><p>Return a Boolean value, i.e. one of <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code>. <em>x</em> is converted
  390. using the standard <a class="reference internal" href="stdtypes.html#truth"><span class="std std-ref">truth testing procedure</span></a>. If <em>x</em> is false
  391. or omitted, this returns <code class="docutils literal notranslate"><span class="pre">False</span></code>; otherwise, it returns <code class="docutils literal notranslate"><span class="pre">True</span></code>. The
  392. <a class="reference internal" href="#bool" title="bool"><code class="xref py py-class docutils literal notranslate"><span class="pre">bool</span></code></a> class is a subclass of <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> (see <a class="reference internal" href="stdtypes.html#typesnumeric"><span class="std std-ref">Numeric Types — int, float, complex</span></a>).
  393. It cannot be subclassed further. Its only instances are <code class="docutils literal notranslate"><span class="pre">False</span></code> and
  394. <code class="docutils literal notranslate"><span class="pre">True</span></code> (see <a class="reference internal" href="stdtypes.html#typebool"><span class="std std-ref">Boolean Type - bool</span></a>).</p>
  395. <div class="versionchanged" id="index-0">
  396. <p><span class="versionmodified changed">Changed in version 3.7: </span><em>x</em> is now a positional-only parameter.</p>
  397. </div>
  398. </dd></dl>
  399. <dl class="py function">
  400. <dt class="sig sig-object py" id="breakpoint">
  401. <span class="sig-name descname"><span class="pre">breakpoint</span></span><span class="sig-paren">(</span><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="o"><span class="pre">**</span></span><span class="n"><span class="pre">kws</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#breakpoint" title="Link to this definition">¶</a></dt>
  402. <dd><p>This function drops you into the debugger at the call site. Specifically,
  403. it calls <a class="reference internal" href="sys.html#sys.breakpointhook" title="sys.breakpointhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.breakpointhook()</span></code></a>, passing <code class="docutils literal notranslate"><span class="pre">args</span></code> and <code class="docutils literal notranslate"><span class="pre">kws</span></code> straight
  404. through. By default, <code class="docutils literal notranslate"><span class="pre">sys.breakpointhook()</span></code> calls
  405. <a class="reference internal" href="pdb.html#pdb.set_trace" title="pdb.set_trace"><code class="xref py py-func docutils literal notranslate"><span class="pre">pdb.set_trace()</span></code></a> expecting no arguments. In this case, it is
  406. purely a convenience function so you don’t have to explicitly import
  407. <a class="reference internal" href="pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a> or type as much code to enter the debugger. However,
  408. <a class="reference internal" href="sys.html#sys.breakpointhook" title="sys.breakpointhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.breakpointhook()</span></code></a> can be set to some other function and
  409. <a class="reference internal" href="#breakpoint" title="breakpoint"><code class="xref py py-func docutils literal notranslate"><span class="pre">breakpoint()</span></code></a> will automatically call that, allowing you to drop into
  410. the debugger of choice.
  411. If <a class="reference internal" href="sys.html#sys.breakpointhook" title="sys.breakpointhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.breakpointhook()</span></code></a> is not accessible, this function will
  412. raise <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>.</p>
  413. <p>By default, the behavior of <a class="reference internal" href="#breakpoint" title="breakpoint"><code class="xref py py-func docutils literal notranslate"><span class="pre">breakpoint()</span></code></a> can be changed with
  414. the <span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONBREAKPOINT"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONBREAKPOINT</span></code></a> environment variable.
  415. See <a class="reference internal" href="sys.html#sys.breakpointhook" title="sys.breakpointhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.breakpointhook()</span></code></a> for usage details.</p>
  416. <p>Note that this is not guaranteed if <a class="reference internal" href="sys.html#sys.breakpointhook" title="sys.breakpointhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.breakpointhook()</span></code></a>
  417. has been replaced.</p>
  418. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">builtins.breakpoint</span></code> with argument <code class="docutils literal notranslate"><span class="pre">breakpointhook</span></code>.</p>
  419. <div class="versionadded">
  420. <p><span class="versionmodified added">New in version 3.7.</span></p>
  421. </div>
  422. </dd></dl>
  423. <dl class="py class" id="func-bytearray">
  424. <dt class="sig sig-object py">
  425. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">bytearray</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em><span class="sig-paren">)</span></dt>
  426. <dt class="sig sig-object py">
  427. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">bytearray</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span></dt>
  428. <dt class="sig sig-object py">
  429. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">bytearray</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span></em><span class="sig-paren">)</span></dt>
  430. <dd><p>Return a new array of bytes. The <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> class is a mutable
  431. sequence of integers in the range 0 &lt;= x &lt; 256. It has most of the usual
  432. methods of mutable sequences, described in <a class="reference internal" href="stdtypes.html#typesseq-mutable"><span class="std std-ref">Mutable Sequence Types</span></a>, as well
  433. as most methods that the <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> type has, see <a class="reference internal" href="stdtypes.html#bytes-methods"><span class="std std-ref">Bytes and Bytearray Operations</span></a>.</p>
  434. <p>The optional <em>source</em> parameter can be used to initialize the array in a few
  435. different ways:</p>
  436. <ul class="simple">
  437. <li><p>If it is a <em>string</em>, you must also give the <em>encoding</em> (and optionally,
  438. <em>errors</em>) parameters; <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-func docutils literal notranslate"><span class="pre">bytearray()</span></code></a> then converts the string to
  439. bytes using <a class="reference internal" href="stdtypes.html#str.encode" title="str.encode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.encode()</span></code></a>.</p></li>
  440. <li><p>If it is an <em>integer</em>, the array will have that size and will be
  441. initialized with null bytes.</p></li>
  442. <li><p>If it is an object conforming to the <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">buffer interface</span></a>,
  443. a read-only buffer of the object will be used to initialize the bytes array.</p></li>
  444. <li><p>If it is an <em>iterable</em>, it must be an iterable of integers in the range
  445. <code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">256</span></code>, which are used as the initial contents of the array.</p></li>
  446. </ul>
  447. <p>Without an argument, an array of size 0 is created.</p>
  448. <p>See also <a class="reference internal" href="stdtypes.html#binaryseq"><span class="std std-ref">Binary Sequence Types — bytes, bytearray, memoryview</span></a> and <a class="reference internal" href="stdtypes.html#typebytearray"><span class="std std-ref">Bytearray Objects</span></a>.</p>
  449. </dd></dl>
  450. <dl class="py class" id="func-bytes">
  451. <dt class="sig sig-object py">
  452. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">bytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em><span class="sig-paren">)</span></dt>
  453. <dt class="sig sig-object py">
  454. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">bytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span></dt>
  455. <dt class="sig sig-object py">
  456. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">bytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span></em><span class="sig-paren">)</span></dt>
  457. <dd><p>Return a new “bytes” object which is an immutable sequence of integers in
  458. the range <code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">256</span></code>. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> is an immutable version of
  459. <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> – it has the same non-mutating methods and the same
  460. indexing and slicing behavior.</p>
  461. <p>Accordingly, constructor arguments are interpreted as for <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-func docutils literal notranslate"><span class="pre">bytearray()</span></code></a>.</p>
  462. <p>Bytes objects can also be created with literals, see <a class="reference internal" href="../reference/lexical_analysis.html#strings"><span class="std std-ref">String and Bytes literals</span></a>.</p>
  463. <p>See also <a class="reference internal" href="stdtypes.html#binaryseq"><span class="std std-ref">Binary Sequence Types — bytes, bytearray, memoryview</span></a>, <a class="reference internal" href="stdtypes.html#typebytes"><span class="std std-ref">Bytes Objects</span></a>, and <a class="reference internal" href="stdtypes.html#bytes-methods"><span class="std std-ref">Bytes and Bytearray Operations</span></a>.</p>
  464. </dd></dl>
  465. <dl class="py function">
  466. <dt class="sig sig-object py" id="callable">
  467. <span class="sig-name descname"><span class="pre">callable</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="#callable" title="Link to this definition">¶</a></dt>
  468. <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if the <em>object</em> argument appears callable,
  469. <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> if not. If this returns <code class="docutils literal notranslate"><span class="pre">True</span></code>, it is still possible that a
  470. call fails, but if it is <code class="docutils literal notranslate"><span class="pre">False</span></code>, calling <em>object</em> will never succeed.
  471. Note that classes are callable (calling a class returns a new instance);
  472. instances are callable if their class has a <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__call__()</span></code></a> method.</p>
  473. <div class="versionadded">
  474. <p><span class="versionmodified added">New in version 3.2: </span>This function was first removed in Python 3.0 and then brought back
  475. in Python 3.2.</p>
  476. </div>
  477. </dd></dl>
  478. <dl class="py function">
  479. <dt class="sig sig-object py" id="chr">
  480. <span class="sig-name descname"><span class="pre">chr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">i</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#chr" title="Link to this definition">¶</a></dt>
  481. <dd><p>Return the string representing a character whose Unicode code point is the
  482. integer <em>i</em>. For example, <code class="docutils literal notranslate"><span class="pre">chr(97)</span></code> returns the string <code class="docutils literal notranslate"><span class="pre">'a'</span></code>, while
  483. <code class="docutils literal notranslate"><span class="pre">chr(8364)</span></code> returns the string <code class="docutils literal notranslate"><span class="pre">'€'</span></code>. This is the inverse of <a class="reference internal" href="#ord" title="ord"><code class="xref py py-func docutils literal notranslate"><span class="pre">ord()</span></code></a>.</p>
  484. <p>The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in
  485. base 16). <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> will be raised if <em>i</em> is outside that range.</p>
  486. </dd></dl>
  487. <dl class="py function">
  488. <dt class="sig sig-object py" id="classmethod">
  489. <span class="sig-prename descclassname"><span class="pre">&#64;</span></span><span class="sig-name descname"><span class="pre">classmethod</span></span><a class="headerlink" href="#classmethod" title="Link to this definition">¶</a></dt>
  490. <dd><p>Transform a method into a class method.</p>
  491. <p>A class method receives the class as an implicit first argument, just like an
  492. instance method receives the instance. To declare a class method, use this
  493. idiom:</p>
  494. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
  495. <span class="nd">@classmethod</span>
  496. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">):</span> <span class="o">...</span>
  497. </pre></div>
  498. </div>
  499. <p>The <code class="docutils literal notranslate"><span class="pre">&#64;classmethod</span></code> form is a function <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a> – see
  500. <a class="reference internal" href="../reference/compound_stmts.html#function"><span class="std std-ref">Function definitions</span></a> for details.</p>
  501. <p>A class method can be called either on the class (such as <code class="docutils literal notranslate"><span class="pre">C.f()</span></code>) or on an instance (such
  502. as <code class="docutils literal notranslate"><span class="pre">C().f()</span></code>). The instance is ignored except for its class. If a class
  503. method is called for a derived class, the derived class object is passed as the
  504. implied first argument.</p>
  505. <p>Class methods are different than C++ or Java static methods. If you want those,
  506. see <a class="reference internal" href="#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a> in this section.
  507. For more information on class methods, see <a class="reference internal" href="../reference/datamodel.html#types"><span class="std std-ref">The standard type hierarchy</span></a>.</p>
  508. <div class="versionchanged">
  509. <p><span class="versionmodified changed">Changed in version 3.9: </span>Class methods can now wrap other <a class="reference internal" href="../glossary.html#term-descriptor"><span class="xref std std-term">descriptors</span></a> such as
  510. <a class="reference internal" href="#property" title="property"><code class="xref py py-func docutils literal notranslate"><span class="pre">property()</span></code></a>.</p>
  511. </div>
  512. <div class="versionchanged">
  513. <p><span class="versionmodified changed">Changed in version 3.10: </span>Class methods now inherit the method attributes (<code class="docutils literal notranslate"><span class="pre">__module__</span></code>,
  514. <code class="docutils literal notranslate"><span class="pre">__name__</span></code>, <code class="docutils literal notranslate"><span class="pre">__qualname__</span></code>, <code class="docutils literal notranslate"><span class="pre">__doc__</span></code> and <code class="docutils literal notranslate"><span class="pre">__annotations__</span></code>) and
  515. have a new <code class="docutils literal notranslate"><span class="pre">__wrapped__</span></code> attribute.</p>
  516. </div>
  517. <div class="versionchanged">
  518. <p><span class="versionmodified changed">Changed in version 3.11: </span>Class methods can no longer wrap other <a class="reference internal" href="../glossary.html#term-descriptor"><span class="xref std std-term">descriptors</span></a> such as
  519. <a class="reference internal" href="#property" title="property"><code class="xref py py-func docutils literal notranslate"><span class="pre">property()</span></code></a>.</p>
  520. </div>
  521. </dd></dl>
  522. <dl class="py function">
  523. <dt class="sig sig-object py" id="compile">
  524. <span class="sig-name descname"><span class="pre">compile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dont_inherit</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">optimize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#compile" title="Link to this definition">¶</a></dt>
  525. <dd><p>Compile the <em>source</em> into a code or AST object. Code objects can be executed
  526. by <a class="reference internal" href="#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> or <a class="reference internal" href="#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>. <em>source</em> can either be a normal string, a
  527. byte string, or an AST object. Refer to the <a class="reference internal" href="ast.html#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code></a> module documentation
  528. for information on how to work with AST objects.</p>
  529. <p>The <em>filename</em> argument should give the file from which the code was read;
  530. pass some recognizable value if it wasn’t read from a file (<code class="docutils literal notranslate"><span class="pre">'&lt;string&gt;'</span></code> is
  531. commonly used).</p>
  532. <p>The <em>mode</em> argument specifies what kind of code must be compiled; it can be
  533. <code class="docutils literal notranslate"><span class="pre">'exec'</span></code> if <em>source</em> consists of a sequence of statements, <code class="docutils literal notranslate"><span class="pre">'eval'</span></code> if it
  534. consists of a single expression, or <code class="docutils literal notranslate"><span class="pre">'single'</span></code> if it consists of a single
  535. interactive statement (in the latter case, expression statements that
  536. evaluate to something other than <code class="docutils literal notranslate"><span class="pre">None</span></code> will be printed).</p>
  537. <p>The optional arguments <em>flags</em> and <em>dont_inherit</em> control which
  538. <a class="reference internal" href="ast.html#ast-compiler-flags"><span class="std std-ref">compiler options</span></a> should be activated
  539. and which <a class="reference internal" href="../reference/simple_stmts.html#future"><span class="std std-ref">future features</span></a> should be allowed. If neither
  540. is present (or both are zero) the code is compiled with the same flags that
  541. affect the code that is calling <a class="reference internal" href="#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>. If the <em>flags</em>
  542. argument is given and <em>dont_inherit</em> is not (or is zero) then the compiler
  543. options and the future statements specified by the <em>flags</em> argument are used
  544. in addition to those that would be used anyway. If <em>dont_inherit</em> is a
  545. non-zero integer then the <em>flags</em> argument is it – the flags (future
  546. features and compiler options) in the surrounding code are ignored.</p>
  547. <p>Compiler options and future statements are specified by bits which can be
  548. bitwise ORed together to specify multiple options. The bitfield required to
  549. specify a given future feature can be found as the
  550. <a class="reference internal" href="__future__.html#future__._Feature.compiler_flag" title="__future__._Feature.compiler_flag"><code class="xref py py-attr docutils literal notranslate"><span class="pre">compiler_flag</span></code></a> attribute on the
  551. <a class="reference internal" href="__future__.html#future__._Feature" title="__future__._Feature"><code class="xref py py-class docutils literal notranslate"><span class="pre">_Feature</span></code></a> instance in the <a class="reference internal" href="__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> module.
  552. <a class="reference internal" href="ast.html#ast-compiler-flags"><span class="std std-ref">Compiler flags</span></a> can be found in <a class="reference internal" href="ast.html#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code></a>
  553. module, with <code class="docutils literal notranslate"><span class="pre">PyCF_</span></code> prefix.</p>
  554. <p>The argument <em>optimize</em> specifies the optimization level of the compiler; the
  555. default value of <code class="docutils literal notranslate"><span class="pre">-1</span></code> selects the optimization level of the interpreter as
  556. given by <a class="reference internal" href="../using/cmdline.html#cmdoption-O"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a> options. Explicit levels are <code class="docutils literal notranslate"><span class="pre">0</span></code> (no optimization;
  557. <code class="docutils literal notranslate"><span class="pre">__debug__</span></code> is true), <code class="docutils literal notranslate"><span class="pre">1</span></code> (asserts are removed, <code class="docutils literal notranslate"><span class="pre">__debug__</span></code> is false)
  558. or <code class="docutils literal notranslate"><span class="pre">2</span></code> (docstrings are removed too).</p>
  559. <p>This function raises <a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> if the compiled source is invalid,
  560. and <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if the source contains null bytes.</p>
  561. <p>If you want to parse Python code into its AST representation, see
  562. <a class="reference internal" href="ast.html#ast.parse" title="ast.parse"><code class="xref py py-func docutils literal notranslate"><span class="pre">ast.parse()</span></code></a>.</p>
  563. <p class="audit-hook"><p>Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">compile</span></code> with arguments
  564. <code class="docutils literal notranslate"><span class="pre">source</span></code> and <code class="docutils literal notranslate"><span class="pre">filename</span></code>. This event may also be raised by implicit
  565. compilation.</p>
  566. </p>
  567. <div class="admonition note">
  568. <p class="admonition-title">Note</p>
  569. <p>When compiling a string with multi-line code in <code class="docutils literal notranslate"><span class="pre">'single'</span></code> or
  570. <code class="docutils literal notranslate"><span class="pre">'eval'</span></code> mode, input must be terminated by at least one newline
  571. character. This is to facilitate detection of incomplete and complete
  572. statements in the <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code></a> module.</p>
  573. </div>
  574. <div class="admonition warning">
  575. <p class="admonition-title">Warning</p>
  576. <p>It is possible to crash the Python interpreter with a
  577. sufficiently large/complex string when compiling to an AST
  578. object due to stack depth limitations in Python’s AST compiler.</p>
  579. </div>
  580. <div class="versionchanged">
  581. <p><span class="versionmodified changed">Changed in version 3.2: </span>Allowed use of Windows and Mac newlines. Also, input in <code class="docutils literal notranslate"><span class="pre">'exec'</span></code> mode
  582. does not have to end in a newline anymore. Added the <em>optimize</em> parameter.</p>
  583. </div>
  584. <div class="versionchanged">
  585. <p><span class="versionmodified changed">Changed in version 3.5: </span>Previously, <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> was raised when null bytes were encountered
  586. in <em>source</em>.</p>
  587. </div>
  588. <div class="versionadded">
  589. <p><span class="versionmodified added">New in version 3.8: </span><code class="docutils literal notranslate"><span class="pre">ast.PyCF_ALLOW_TOP_LEVEL_AWAIT</span></code> can now be passed in flags to enable
  590. support for top-level <code class="docutils literal notranslate"><span class="pre">await</span></code>, <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">for</span></code>, and <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span></code>.</p>
  591. </div>
  592. </dd></dl>
  593. <dl class="py class">
  594. <dt class="sig sig-object py" id="complex">
  595. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">complex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">real</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">imag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#complex" title="Link to this definition">¶</a></dt>
  596. <dt class="sig sig-object py">
  597. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">complex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="sig-paren">)</span></dt>
  598. <dd><p>Return a complex number with the value <em>real</em> + <em>imag</em>*1j or convert a string
  599. or number to a complex number. If the first parameter is a string, it will
  600. be interpreted as a complex number and the function must be called without a
  601. second parameter. The second parameter can never be a string. Each argument
  602. may be any numeric type (including complex). If <em>imag</em> is omitted, it
  603. defaults to zero and the constructor serves as a numeric conversion like
  604. <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> and <a class="reference internal" href="#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>. If both arguments are omitted, returns
  605. <code class="docutils literal notranslate"><span class="pre">0j</span></code>.</p>
  606. <p>For a general Python object <code class="docutils literal notranslate"><span class="pre">x</span></code>, <code class="docutils literal notranslate"><span class="pre">complex(x)</span></code> delegates to
  607. <code class="docutils literal notranslate"><span class="pre">x.__complex__()</span></code>. If <a class="reference internal" href="../reference/datamodel.html#object.__complex__" title="object.__complex__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__complex__()</span></code></a> is not defined then it falls back
  608. to <a class="reference internal" href="../reference/datamodel.html#object.__float__" title="object.__float__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__float__()</span></code></a>. If <code class="xref py py-meth docutils literal notranslate"><span class="pre">__float__()</span></code> is not defined then it falls back
  609. to <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a>.</p>
  610. <div class="admonition note">
  611. <p class="admonition-title">Note</p>
  612. <p>When converting from a string, the string must not contain whitespace
  613. around the central <code class="docutils literal notranslate"><span class="pre">+</span></code> or <code class="docutils literal notranslate"><span class="pre">-</span></code> operator. For example,
  614. <code class="docutils literal notranslate"><span class="pre">complex('1+2j')</span></code> is fine, but <code class="docutils literal notranslate"><span class="pre">complex('1</span> <span class="pre">+</span> <span class="pre">2j')</span></code> raises
  615. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  616. </div>
  617. <p>The complex type is described in <a class="reference internal" href="stdtypes.html#typesnumeric"><span class="std std-ref">Numeric Types — int, float, complex</span></a>.</p>
  618. <div class="versionchanged">
  619. <p><span class="versionmodified changed">Changed in version 3.6: </span>Grouping digits with underscores as in code literals is allowed.</p>
  620. </div>
  621. <div class="versionchanged">
  622. <p><span class="versionmodified changed">Changed in version 3.8: </span>Falls back to <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> if <a class="reference internal" href="../reference/datamodel.html#object.__complex__" title="object.__complex__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__complex__()</span></code></a> and
  623. <a class="reference internal" href="../reference/datamodel.html#object.__float__" title="object.__float__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__float__()</span></code></a> are not defined.</p>
  624. </div>
  625. </dd></dl>
  626. <dl class="py function">
  627. <dt class="sig sig-object py" id="delattr">
  628. <span class="sig-name descname"><span class="pre">delattr</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">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#delattr" title="Link to this definition">¶</a></dt>
  629. <dd><p>This is a relative of <a class="reference internal" href="#setattr" title="setattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">setattr()</span></code></a>. The arguments are an object and a
  630. string. The string must be the name of one of the object’s attributes. The
  631. function deletes the named attribute, provided the object allows it. For
  632. example, <code class="docutils literal notranslate"><span class="pre">delattr(x,</span> <span class="pre">'foobar')</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">x.foobar</span></code>.
  633. <em>name</em> need not be a Python identifier (see <a class="reference internal" href="#setattr" title="setattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">setattr()</span></code></a>).</p>
  634. </dd></dl>
  635. <dl class="py class" id="func-dict">
  636. <dt class="sig sig-object py">
  637. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">dict</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwarg</span></span></em><span class="sig-paren">)</span></dt>
  638. <dt class="sig sig-object py">
  639. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">dict</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mapping</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwarg</span></span></em><span class="sig-paren">)</span></dt>
  640. <dt class="sig sig-object py">
  641. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">dict</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwarg</span></span></em><span class="sig-paren">)</span></dt>
  642. <dd><p>Create a new dictionary. The <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> object is the dictionary class.
  643. See <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> and <a class="reference internal" href="stdtypes.html#typesmapping"><span class="std std-ref">Mapping Types — dict</span></a> for documentation about this class.</p>
  644. <p>For other containers see the built-in <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>, <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a>, and
  645. <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> classes, as well as the <a class="reference internal" href="collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module.</p>
  646. </dd></dl>
  647. <dl class="py function">
  648. <dt class="sig sig-object py" id="dir">
  649. <span class="sig-name descname"><span class="pre">dir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#dir" title="Link to this definition">¶</a></dt>
  650. <dt class="sig sig-object py">
  651. <span class="sig-name descname"><span class="pre">dir</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></dt>
  652. <dd><p>Without arguments, return the list of names in the current local scope. With an
  653. argument, attempt to return a list of valid attributes for that object.</p>
  654. <p>If the object has a method named <a class="reference internal" href="../reference/datamodel.html#object.__dir__" title="object.__dir__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__dir__()</span></code></a>,
  655. this method will be called and
  656. must return the list of attributes. This allows objects that implement a custom
  657. <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__getattr__()</span></code></a> or <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__getattribute__()</span></code></a> function
  658. to customize the way
  659. <a class="reference internal" href="#dir" title="dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code></a> reports their attributes.</p>
  660. <p>If the object does not provide <a class="reference internal" href="../reference/datamodel.html#object.__dir__" title="object.__dir__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__dir__()</span></code></a>,
  661. the function tries its best to gather information from the object’s
  662. <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> attribute, if defined, and
  663. from its type object. The resulting list is not necessarily complete and may
  664. be inaccurate when the object has a custom <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__getattr__()</span></code></a>.</p>
  665. <p>The default <a class="reference internal" href="#dir" title="dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code></a> mechanism behaves differently with different types of
  666. objects, as it attempts to produce the most relevant, rather than complete,
  667. information:</p>
  668. <ul class="simple">
  669. <li><p>If the object is a module object, the list contains the names of the module’s
  670. attributes.</p></li>
  671. <li><p>If the object is a type or class object, the list contains the names of its
  672. attributes, and recursively of the attributes of its bases.</p></li>
  673. <li><p>Otherwise, the list contains the object’s attributes’ names, the names of its
  674. class’s attributes, and recursively of the attributes of its class’s base
  675. classes.</p></li>
  676. </ul>
  677. <p>The resulting list is sorted alphabetically. For example:</p>
  678. <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">struct</span>
  679. <span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">()</span> <span class="c1"># show the names in the module namespace </span>
  680. <span class="go">[&#39;__builtins__&#39;, &#39;__name__&#39;, &#39;struct&#39;]</span>
  681. <span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">(</span><span class="n">struct</span><span class="p">)</span> <span class="c1"># show the names in the struct module </span>
  682. <span class="go">[&#39;Struct&#39;, &#39;__all__&#39;, &#39;__builtins__&#39;, &#39;__cached__&#39;, &#39;__doc__&#39;, &#39;__file__&#39;,</span>
  683. <span class="go"> &#39;__initializing__&#39;, &#39;__loader__&#39;, &#39;__name__&#39;, &#39;__package__&#39;,</span>
  684. <span class="go"> &#39;_clearcache&#39;, &#39;calcsize&#39;, &#39;error&#39;, &#39;pack&#39;, &#39;pack_into&#39;,</span>
  685. <span class="go"> &#39;unpack&#39;, &#39;unpack_from&#39;]</span>
  686. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Shape</span><span class="p">:</span>
  687. <span class="gp">... </span> <span class="k">def</span> <span class="fm">__dir__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  688. <span class="gp">... </span> <span class="k">return</span> <span class="p">[</span><span class="s1">&#39;area&#39;</span><span class="p">,</span> <span class="s1">&#39;perimeter&#39;</span><span class="p">,</span> <span class="s1">&#39;location&#39;</span><span class="p">]</span>
  689. <span class="gp">...</span>
  690. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">Shape</span><span class="p">()</span>
  691. <span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  692. <span class="go">[&#39;area&#39;, &#39;location&#39;, &#39;perimeter&#39;]</span>
  693. </pre></div>
  694. </div>
  695. <div class="admonition note">
  696. <p class="admonition-title">Note</p>
  697. <p>Because <a class="reference internal" href="#dir" title="dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code></a> is supplied primarily as a convenience for use at an
  698. interactive prompt, it tries to supply an interesting set of names more
  699. than it tries to supply a rigorously or consistently defined set of names,
  700. and its detailed behavior may change across releases. For example,
  701. metaclass attributes are not in the result list when the argument is a
  702. class.</p>
  703. </div>
  704. </dd></dl>
  705. <dl class="py function">
  706. <dt class="sig sig-object py" id="divmod">
  707. <span class="sig-name descname"><span class="pre">divmod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#divmod" title="Link to this definition">¶</a></dt>
  708. <dd><p>Take two (non-complex) numbers as arguments and return a pair of numbers
  709. consisting of their quotient and remainder when using integer division. With
  710. mixed operand types, the rules for binary arithmetic operators apply. For
  711. integers, the result is the same as <code class="docutils literal notranslate"><span class="pre">(a</span> <span class="pre">//</span> <span class="pre">b,</span> <span class="pre">a</span> <span class="pre">%</span> <span class="pre">b)</span></code>. For floating point
  712. numbers the result is <code class="docutils literal notranslate"><span class="pre">(q,</span> <span class="pre">a</span> <span class="pre">%</span> <span class="pre">b)</span></code>, where <em>q</em> is usually <code class="docutils literal notranslate"><span class="pre">math.floor(a</span> <span class="pre">/</span>
  713. <span class="pre">b)</span></code> but may be 1 less than that. In any case <code class="docutils literal notranslate"><span class="pre">q</span> <span class="pre">*</span> <span class="pre">b</span> <span class="pre">+</span> <span class="pre">a</span> <span class="pre">%</span> <span class="pre">b</span></code> is very
  714. close to <em>a</em>, if <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">%</span> <span class="pre">b</span></code> is non-zero it has the same sign as <em>b</em>, and <code class="docutils literal notranslate"><span class="pre">0</span>
  715. <span class="pre">&lt;=</span> <span class="pre">abs(a</span> <span class="pre">%</span> <span class="pre">b)</span> <span class="pre">&lt;</span> <span class="pre">abs(b)</span></code>.</p>
  716. </dd></dl>
  717. <dl class="py function">
  718. <dt class="sig sig-object py" id="enumerate">
  719. <span class="sig-name descname"><span class="pre">enumerate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">start</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#enumerate" title="Link to this definition">¶</a></dt>
  720. <dd><p>Return an enumerate object. <em>iterable</em> must be a sequence, an
  721. <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a>, or some other object which supports iteration.
  722. The <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> method of the iterator returned by
  723. <a class="reference internal" href="#enumerate" title="enumerate"><code class="xref py py-func docutils literal notranslate"><span class="pre">enumerate()</span></code></a> returns a tuple containing a count (from <em>start</em> which
  724. defaults to 0) and the values obtained from iterating over <em>iterable</em>.</p>
  725. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">seasons</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Spring&#39;</span><span class="p">,</span> <span class="s1">&#39;Summer&#39;</span><span class="p">,</span> <span class="s1">&#39;Fall&#39;</span><span class="p">,</span> <span class="s1">&#39;Winter&#39;</span><span class="p">]</span>
  726. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">seasons</span><span class="p">))</span>
  727. <span class="go">[(0, &#39;Spring&#39;), (1, &#39;Summer&#39;), (2, &#39;Fall&#39;), (3, &#39;Winter&#39;)]</span>
  728. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">seasons</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
  729. <span class="go">[(1, &#39;Spring&#39;), (2, &#39;Summer&#39;), (3, &#39;Fall&#39;), (4, &#39;Winter&#39;)]</span>
  730. </pre></div>
  731. </div>
  732. <p>Equivalent to:</p>
  733. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">enumerate</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
  734. <span class="n">n</span> <span class="o">=</span> <span class="n">start</span>
  735. <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
  736. <span class="k">yield</span> <span class="n">n</span><span class="p">,</span> <span class="n">elem</span>
  737. <span class="n">n</span> <span class="o">+=</span> <span class="mi">1</span>
  738. </pre></div>
  739. </div>
  740. </dd></dl>
  741. <dl class="py function" id="func-eval">
  742. <dt class="sig sig-object py" id="eval">
  743. <span class="sig-name descname"><span class="pre">eval</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">expression</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globals</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">locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#eval" title="Link to this definition">¶</a></dt>
  744. <dd><p>The arguments are a string and optional globals and locals. If provided,
  745. <em>globals</em> must be a dictionary. If provided, <em>locals</em> can be any mapping
  746. object.</p>
  747. <p>The <em>expression</em> argument is parsed and evaluated as a Python expression
  748. (technically speaking, a condition list) using the <em>globals</em> and <em>locals</em>
  749. dictionaries as global and local namespace. If the <em>globals</em> dictionary is
  750. present and does not contain a value for the key <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code>, a
  751. reference to the dictionary of the built-in module <a class="reference internal" href="builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> is
  752. inserted under that key before <em>expression</em> is parsed. That way you can
  753. control what builtins are available to the executed code by inserting your
  754. own <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> dictionary into <em>globals</em> before passing it to
  755. <a class="reference internal" href="#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>. If the <em>locals</em> dictionary is omitted it defaults to the
  756. <em>globals</em> dictionary. If both dictionaries are omitted, the expression is
  757. executed with the <em>globals</em> and <em>locals</em> in the environment where
  758. <a class="reference internal" href="#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> is called. Note, <em>eval()</em> does not have access to the
  759. <a class="reference internal" href="../glossary.html#term-nested-scope"><span class="xref std std-term">nested scopes</span></a> (non-locals) in the enclosing
  760. environment.</p>
  761. <p>The return value is the result of
  762. the evaluated expression. Syntax errors are reported as exceptions. Example:</p>
  763. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
  764. <span class="gp">&gt;&gt;&gt; </span><span class="nb">eval</span><span class="p">(</span><span class="s1">&#39;x+1&#39;</span><span class="p">)</span>
  765. <span class="go">2</span>
  766. </pre></div>
  767. </div>
  768. <p>This function can also be used to execute arbitrary code objects (such as
  769. those created by <a class="reference internal" href="#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>). In this case, pass a code object instead
  770. of a string. If the code object has been compiled with <code class="docutils literal notranslate"><span class="pre">'exec'</span></code> as the
  771. <em>mode</em> argument, <a class="reference internal" href="#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>'s return value will be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  772. <p>Hints: dynamic execution of statements is supported by the <a class="reference internal" href="#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a>
  773. function. The <a class="reference internal" href="#globals" title="globals"><code class="xref py py-func docutils literal notranslate"><span class="pre">globals()</span></code></a> and <a class="reference internal" href="#locals" title="locals"><code class="xref py py-func docutils literal notranslate"><span class="pre">locals()</span></code></a> functions
  774. return the current global and local dictionary, respectively, which may be
  775. useful to pass around for use by <a class="reference internal" href="#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> or <a class="reference internal" href="#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a>.</p>
  776. <p>If the given source is a string, then leading and trailing spaces and tabs
  777. are stripped.</p>
  778. <p>See <a class="reference internal" href="ast.html#ast.literal_eval" title="ast.literal_eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">ast.literal_eval()</span></code></a> for a function that can safely evaluate strings
  779. with expressions containing only literals.</p>
  780. <p class="audit-hook"><p>Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">exec</span></code> with the code object
  781. as the argument. Code compilation events may also be raised.</p>
  782. </p>
  783. </dd></dl>
  784. <dl class="py function" id="index-2">
  785. <dt class="sig sig-object py" id="exec">
  786. <span class="sig-name descname"><span class="pre">exec</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">globals</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">locals</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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">closure</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#exec" title="Link to this definition">¶</a></dt>
  787. <dd><p>This function supports dynamic execution of Python code. <em>object</em> must be
  788. either a string or a code object. If it is a string, the string is parsed as
  789. a suite of Python statements which is then executed (unless a syntax error
  790. occurs). <a class="footnote-reference brackets" href="#id2" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> If it is a code object, it is simply executed. In all cases,
  791. the code that’s executed is expected to be valid as file input (see the
  792. section <a class="reference internal" href="../reference/toplevel_components.html#file-input"><span class="std std-ref">File input</span></a> in the Reference Manual). Be aware that the
  793. <a class="reference internal" href="../reference/simple_stmts.html#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a>, <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a>, and <a class="reference internal" href="../reference/simple_stmts.html#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a>
  794. statements may not be used outside of
  795. function definitions even within the context of code passed to the
  796. <a class="reference internal" href="#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> function. The return value is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  797. <p>In all cases, if the optional parts are omitted, the code is executed in the
  798. current scope. If only <em>globals</em> is provided, it must be a dictionary
  799. (and not a subclass of dictionary), which
  800. will be used for both the global and the local variables. If <em>globals</em> and
  801. <em>locals</em> are given, they are used for the global and local variables,
  802. respectively. If provided, <em>locals</em> can be any mapping object. Remember
  803. that at the module level, globals and locals are the same dictionary. If exec
  804. gets two separate objects as <em>globals</em> and <em>locals</em>, the code will be
  805. executed as if it were embedded in a class definition.</p>
  806. <p>If the <em>globals</em> dictionary does not contain a value for the key
  807. <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code>, a reference to the dictionary of the built-in module
  808. <a class="reference internal" href="builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> is inserted under that key. That way you can control what
  809. builtins are available to the executed code by inserting your own
  810. <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> dictionary into <em>globals</em> before passing it to <a class="reference internal" href="#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a>.</p>
  811. <p>The <em>closure</em> argument specifies a closure–a tuple of cellvars.
  812. It’s only valid when the <em>object</em> is a code object containing free variables.
  813. The length of the tuple must exactly match the number of free variables
  814. referenced by the code object.</p>
  815. <p class="audit-hook"><p>Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">exec</span></code> with the code object
  816. as the argument. Code compilation events may also be raised.</p>
  817. </p>
  818. <div class="admonition note">
  819. <p class="admonition-title">Note</p>
  820. <p>The built-in functions <a class="reference internal" href="#globals" title="globals"><code class="xref py py-func docutils literal notranslate"><span class="pre">globals()</span></code></a> and <a class="reference internal" href="#locals" title="locals"><code class="xref py py-func docutils literal notranslate"><span class="pre">locals()</span></code></a> return the current
  821. global and local dictionary, respectively, which may be useful to pass around
  822. for use as the second and third argument to <a class="reference internal" href="#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a>.</p>
  823. </div>
  824. <div class="admonition note">
  825. <p class="admonition-title">Note</p>
  826. <p>The default <em>locals</em> act as described for function <a class="reference internal" href="#locals" title="locals"><code class="xref py py-func docutils literal notranslate"><span class="pre">locals()</span></code></a> below:
  827. modifications to the default <em>locals</em> dictionary should not be attempted.
  828. Pass an explicit <em>locals</em> dictionary if you need to see effects of the
  829. code on <em>locals</em> after function <a class="reference internal" href="#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> returns.</p>
  830. </div>
  831. <div class="versionchanged">
  832. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>closure</em> parameter.</p>
  833. </div>
  834. </dd></dl>
  835. <dl class="py function">
  836. <dt class="sig sig-object py" id="filter">
  837. <span class="sig-name descname"><span class="pre">filter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">function</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#filter" title="Link to this definition">¶</a></dt>
  838. <dd><p>Construct an iterator from those elements of <em>iterable</em> for which <em>function</em>
  839. is true. <em>iterable</em> may be either a sequence, a container which
  840. supports iteration, or an iterator. If <em>function</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the identity
  841. function is assumed, that is, all elements of <em>iterable</em> that are false are
  842. removed.</p>
  843. <p>Note that <code class="docutils literal notranslate"><span class="pre">filter(function,</span> <span class="pre">iterable)</span></code> is equivalent to the generator
  844. expression <code class="docutils literal notranslate"><span class="pre">(item</span> <span class="pre">for</span> <span class="pre">item</span> <span class="pre">in</span> <span class="pre">iterable</span> <span class="pre">if</span> <span class="pre">function(item))</span></code> if function is
  845. not <code class="docutils literal notranslate"><span class="pre">None</span></code> and <code class="docutils literal notranslate"><span class="pre">(item</span> <span class="pre">for</span> <span class="pre">item</span> <span class="pre">in</span> <span class="pre">iterable</span> <span class="pre">if</span> <span class="pre">item)</span></code> if function is
  846. <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  847. <p>See <a class="reference internal" href="itertools.html#itertools.filterfalse" title="itertools.filterfalse"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.filterfalse()</span></code></a> for the complementary function that returns
  848. elements of <em>iterable</em> for which <em>function</em> is false.</p>
  849. </dd></dl>
  850. <dl class="py class">
  851. <dt class="sig sig-object py" id="float">
  852. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">float</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0.0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#float" title="Link to this definition">¶</a></dt>
  853. <dd><p id="index-3">Return a floating point number constructed from a number or string <em>x</em>.</p>
  854. <p>If the argument is a string, it should contain a decimal number, optionally
  855. preceded by a sign, and optionally embedded in whitespace. The optional
  856. sign may be <code class="docutils literal notranslate"><span class="pre">'+'</span></code> or <code class="docutils literal notranslate"><span class="pre">'-'</span></code>; a <code class="docutils literal notranslate"><span class="pre">'+'</span></code> sign has no effect on the value
  857. produced. The argument may also be a string representing a NaN
  858. (not-a-number), or positive or negative infinity. More precisely, the
  859. input must conform to the <code class="docutils literal notranslate"><span class="pre">floatvalue</span></code> production rule in the following
  860. grammar, after leading and trailing whitespace characters are removed:</p>
  861. <pre>
  862. <strong id="grammar-token-float-sign">sign </strong> ::= &quot;+&quot; | &quot;-&quot;
  863. <strong id="grammar-token-float-infinity">infinity </strong> ::= &quot;Infinity&quot; | &quot;inf&quot;
  864. <strong id="grammar-token-float-nan">nan </strong> ::= &quot;nan&quot;
  865. <strong id="grammar-token-float-digit">digit </strong> ::= &lt;a Unicode decimal digit, i.e. characters in Unicode general category Nd&gt;
  866. <strong id="grammar-token-float-digitpart">digitpart </strong> ::= <a class="reference internal" href="#grammar-token-float-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a> ([&quot;_&quot;] <a class="reference internal" href="#grammar-token-float-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>)*
  867. <strong id="grammar-token-float-number">number </strong> ::= [<a class="reference internal" href="#grammar-token-float-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a>] &quot;.&quot; <a class="reference internal" href="#grammar-token-float-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a> | <a class="reference internal" href="#grammar-token-float-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a> [&quot;.&quot;]
  868. <strong id="grammar-token-float-exponent">exponent </strong> ::= (&quot;e&quot; | &quot;E&quot;) [&quot;+&quot; | &quot;-&quot;] <a class="reference internal" href="#grammar-token-float-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a>
  869. <strong id="grammar-token-float-floatnumber">floatnumber</strong> ::= number [<a class="reference internal" href="#grammar-token-float-exponent"><code class="xref docutils literal notranslate"><span class="pre">exponent</span></code></a>]
  870. <strong id="grammar-token-float-floatvalue">floatvalue </strong> ::= [<a class="reference internal" href="#grammar-token-float-sign"><code class="xref docutils literal notranslate"><span class="pre">sign</span></code></a>] (<a class="reference internal" href="#grammar-token-float-floatnumber"><code class="xref docutils literal notranslate"><span class="pre">floatnumber</span></code></a> | <a class="reference internal" href="#grammar-token-float-infinity"><code class="xref docutils literal notranslate"><span class="pre">infinity</span></code></a> | <a class="reference internal" href="#grammar-token-float-nan"><code class="xref docutils literal notranslate"><span class="pre">nan</span></code></a>)
  871. </pre>
  872. <p>Case is not significant, so, for example, “inf”, “Inf”, “INFINITY”, and
  873. “iNfINity” are all acceptable spellings for positive infinity.</p>
  874. <p>Otherwise, if the argument is an integer or a floating point number, a
  875. floating point number with the same value (within Python’s floating point
  876. precision) is returned. If the argument is outside the range of a Python
  877. float, an <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> will be raised.</p>
  878. <p>For a general Python object <code class="docutils literal notranslate"><span class="pre">x</span></code>, <code class="docutils literal notranslate"><span class="pre">float(x)</span></code> delegates to
  879. <code class="docutils literal notranslate"><span class="pre">x.__float__()</span></code>. If <a class="reference internal" href="../reference/datamodel.html#object.__float__" title="object.__float__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__float__()</span></code></a> is not defined then it falls back
  880. to <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a>.</p>
  881. <p>If no argument is given, <code class="docutils literal notranslate"><span class="pre">0.0</span></code> is returned.</p>
  882. <p>Examples:</p>
  883. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="s1">&#39;+1.23&#39;</span><span class="p">)</span>
  884. <span class="go">1.23</span>
  885. <span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="s1">&#39; -12345</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
  886. <span class="go">-12345.0</span>
  887. <span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="s1">&#39;1e-003&#39;</span><span class="p">)</span>
  888. <span class="go">0.001</span>
  889. <span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="s1">&#39;+1E6&#39;</span><span class="p">)</span>
  890. <span class="go">1000000.0</span>
  891. <span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="s1">&#39;-Infinity&#39;</span><span class="p">)</span>
  892. <span class="go">-inf</span>
  893. </pre></div>
  894. </div>
  895. <p>The float type is described in <a class="reference internal" href="stdtypes.html#typesnumeric"><span class="std std-ref">Numeric Types — int, float, complex</span></a>.</p>
  896. <div class="versionchanged">
  897. <p><span class="versionmodified changed">Changed in version 3.6: </span>Grouping digits with underscores as in code literals is allowed.</p>
  898. </div>
  899. <div class="versionchanged">
  900. <p><span class="versionmodified changed">Changed in version 3.7: </span><em>x</em> is now a positional-only parameter.</p>
  901. </div>
  902. <div class="versionchanged">
  903. <p><span class="versionmodified changed">Changed in version 3.8: </span>Falls back to <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> if <a class="reference internal" href="../reference/datamodel.html#object.__float__" title="object.__float__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__float__()</span></code></a> is not defined.</p>
  904. </div>
  905. </dd></dl>
  906. <dl class="py function" id="index-4">
  907. <dt class="sig sig-object py" id="format">
  908. <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">value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">format_spec</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#format" title="Link to this definition">¶</a></dt>
  909. <dd><p>Convert a <em>value</em> to a “formatted” representation, as controlled by
  910. <em>format_spec</em>. The interpretation of <em>format_spec</em> will depend on the type
  911. of the <em>value</em> argument; however, there is a standard formatting syntax that
  912. is used by most built-in types: <a class="reference internal" href="string.html#formatspec"><span class="std std-ref">Format Specification Mini-Language</span></a>.</p>
  913. <p>The default <em>format_spec</em> is an empty string which usually gives the same
  914. effect as calling <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str(value)</span></code></a>.</p>
  915. <p>A call to <code class="docutils literal notranslate"><span class="pre">format(value,</span> <span class="pre">format_spec)</span></code> is translated to
  916. <code class="docutils literal notranslate"><span class="pre">type(value).__format__(value,</span> <span class="pre">format_spec)</span></code> which bypasses the instance
  917. dictionary when searching for the value’s <a class="reference internal" href="../reference/datamodel.html#object.__format__" title="object.__format__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__format__()</span></code></a> method.
  918. A <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception is raised if the method search reaches
  919. <a class="reference internal" href="#object" title="object"><code class="xref py py-mod docutils literal notranslate"><span class="pre">object</span></code></a> and the <em>format_spec</em> is non-empty, or if either the
  920. <em>format_spec</em> or the return value are not strings.</p>
  921. <div class="versionchanged">
  922. <p><span class="versionmodified changed">Changed in version 3.4: </span><code class="docutils literal notranslate"><span class="pre">object().__format__(format_spec)</span></code> raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>
  923. if <em>format_spec</em> is not an empty string.</p>
  924. </div>
  925. </dd></dl>
  926. <dl class="py class" id="func-frozenset">
  927. <dt class="sig sig-object py">
  928. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">frozenset</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">set()</span></span></em><span class="sig-paren">)</span></dt>
  929. <dd><p>Return a new <a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal notranslate"><span class="pre">frozenset</span></code></a> object, optionally with elements taken from
  930. <em>iterable</em>. <code class="docutils literal notranslate"><span class="pre">frozenset</span></code> is a built-in class. See <a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal notranslate"><span class="pre">frozenset</span></code></a> and
  931. <a class="reference internal" href="stdtypes.html#types-set"><span class="std std-ref">Set Types — set, frozenset</span></a> for documentation about this class.</p>
  932. <p>For other containers see the built-in <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</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>,
  933. <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>, and <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> classes, as well as the <a class="reference internal" href="collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a>
  934. module.</p>
  935. </dd></dl>
  936. <dl class="py function">
  937. <dt class="sig sig-object py" id="getattr">
  938. <span class="sig-name descname"><span class="pre">getattr</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">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#getattr" title="Link to this definition">¶</a></dt>
  939. <dt class="sig sig-object py">
  940. <span class="sig-name descname"><span class="pre">getattr</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">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span></em><span class="sig-paren">)</span></dt>
  941. <dd><p>Return the value of the named attribute of <em>object</em>. <em>name</em> must be a string.
  942. If the string is the name of one of the object’s attributes, the result is the
  943. value of that attribute. For example, <code class="docutils literal notranslate"><span class="pre">getattr(x,</span> <span class="pre">'foobar')</span></code> is equivalent to
  944. <code class="docutils literal notranslate"><span class="pre">x.foobar</span></code>. If the named attribute does not exist, <em>default</em> is returned if
  945. provided, otherwise <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> is raised.
  946. <em>name</em> need not be a Python identifier (see <a class="reference internal" href="#setattr" title="setattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">setattr()</span></code></a>).</p>
  947. <div class="admonition note">
  948. <p class="admonition-title">Note</p>
  949. <p>Since <a class="reference internal" href="../reference/expressions.html#private-name-mangling"><span class="std std-ref">private name mangling</span></a> happens at
  950. compilation time, one must manually mangle a private attribute’s
  951. (attributes with two leading underscores) name in order to retrieve it with
  952. <a class="reference internal" href="#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a>.</p>
  953. </div>
  954. </dd></dl>
  955. <dl class="py function">
  956. <dt class="sig sig-object py" id="globals">
  957. <span class="sig-name descname"><span class="pre">globals</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#globals" title="Link to this definition">¶</a></dt>
  958. <dd><p>Return the dictionary implementing the current module namespace. For code within
  959. functions, this is set when the function is defined and remains the same
  960. regardless of where the function is called.</p>
  961. </dd></dl>
  962. <dl class="py function">
  963. <dt class="sig sig-object py" id="hasattr">
  964. <span class="sig-name descname"><span class="pre">hasattr</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">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hasattr" title="Link to this definition">¶</a></dt>
  965. <dd><p>The arguments are an object and a string. The result is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the
  966. string is the name of one of the object’s attributes, <code class="docutils literal notranslate"><span class="pre">False</span></code> if not. (This
  967. is implemented by calling <code class="docutils literal notranslate"><span class="pre">getattr(object,</span> <span class="pre">name)</span></code> and seeing whether it
  968. raises an <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> or not.)</p>
  969. </dd></dl>
  970. <dl class="py function">
  971. <dt class="sig sig-object py" id="hash">
  972. <span class="sig-name descname"><span class="pre">hash</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="#hash" title="Link to this definition">¶</a></dt>
  973. <dd><p>Return the hash value of the object (if it has one). Hash values are
  974. integers. They are used to quickly compare dictionary keys during a
  975. dictionary lookup. Numeric values that compare equal have the same hash
  976. value (even if they are of different types, as is the case for 1 and 1.0).</p>
  977. <div class="admonition note">
  978. <p class="admonition-title">Note</p>
  979. <p>For objects with custom <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code></a> methods,
  980. note that <a class="reference internal" href="#hash" title="hash"><code class="xref py py-func docutils literal notranslate"><span class="pre">hash()</span></code></a>
  981. truncates the return value based on the bit width of the host machine.</p>
  982. </div>
  983. </dd></dl>
  984. <dl class="py function">
  985. <dt class="sig sig-object py" id="help">
  986. <span class="sig-name descname"><span class="pre">help</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#help" title="Link to this definition">¶</a></dt>
  987. <dt class="sig sig-object py">
  988. <span class="sig-name descname"><span class="pre">help</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">request</span></span></em><span class="sig-paren">)</span></dt>
  989. <dd><p>Invoke the built-in help system. (This function is intended for interactive
  990. use.) If no argument is given, the interactive help system starts on the
  991. interpreter console. If the argument is a string, then the string is looked up
  992. as the name of a module, function, class, method, keyword, or documentation
  993. topic, and a help page is printed on the console. If the argument is any other
  994. kind of object, a help page on the object is generated.</p>
  995. <p>Note that if a slash(/) appears in the parameter list of a function when
  996. invoking <a class="reference internal" href="#help" title="help"><code class="xref py py-func docutils literal notranslate"><span class="pre">help()</span></code></a>, it means that the parameters prior to the slash are
  997. positional-only. For more info, see
  998. <a class="reference internal" href="../faq/programming.html#faq-positional-only-arguments"><span class="std std-ref">the FAQ entry on positional-only parameters</span></a>.</p>
  999. <p>This function is added to the built-in namespace by the <a class="reference internal" href="site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> module.</p>
  1000. <div class="versionchanged">
  1001. <p><span class="versionmodified changed">Changed in version 3.4: </span>Changes to <a class="reference internal" href="pydoc.html#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> and <a class="reference internal" href="inspect.html#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code></a> mean that the reported
  1002. signatures for callables are now more comprehensive and consistent.</p>
  1003. </div>
  1004. </dd></dl>
  1005. <dl class="py function">
  1006. <dt class="sig sig-object py" id="hex">
  1007. <span class="sig-name descname"><span class="pre">hex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hex" title="Link to this definition">¶</a></dt>
  1008. <dd><p>Convert an integer number to a lowercase hexadecimal string prefixed with
  1009. “0x”. If <em>x</em> is not a Python <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> object, it has to define an
  1010. <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> method that returns an integer. Some examples:</p>
  1011. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">hex</span><span class="p">(</span><span class="mi">255</span><span class="p">)</span>
  1012. <span class="go">&#39;0xff&#39;</span>
  1013. <span class="gp">&gt;&gt;&gt; </span><span class="nb">hex</span><span class="p">(</span><span class="o">-</span><span class="mi">42</span><span class="p">)</span>
  1014. <span class="go">&#39;-0x2a&#39;</span>
  1015. </pre></div>
  1016. </div>
  1017. <p>If you want to convert an integer number to an uppercase or lower hexadecimal
  1018. string with prefix or not, you can use either of the following ways:</p>
  1019. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">%#x</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="mi">255</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%x</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="mi">255</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%X</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="mi">255</span>
  1020. <span class="go">(&#39;0xff&#39;, &#39;ff&#39;, &#39;FF&#39;)</span>
  1021. <span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="s1">&#39;#x&#39;</span><span class="p">),</span> <span class="nb">format</span><span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="nb">format</span><span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="s1">&#39;X&#39;</span><span class="p">)</span>
  1022. <span class="go">(&#39;0xff&#39;, &#39;ff&#39;, &#39;FF&#39;)</span>
  1023. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="mi">255</span><span class="si">:</span><span class="s1">#x</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="mi">255</span><span class="si">:</span><span class="s1">x</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="mi">255</span><span class="si">:</span><span class="s1">X</span><span class="si">}</span><span class="s1">&#39;</span>
  1024. <span class="go">(&#39;0xff&#39;, &#39;ff&#39;, &#39;FF&#39;)</span>
  1025. </pre></div>
  1026. </div>
  1027. <p>See also <a class="reference internal" href="#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> for more information.</p>
  1028. <p>See also <a class="reference internal" href="#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> for converting a hexadecimal string to an
  1029. integer using a base of 16.</p>
  1030. <div class="admonition note">
  1031. <p class="admonition-title">Note</p>
  1032. <p>To obtain a hexadecimal string representation for a float, use the
  1033. <a class="reference internal" href="stdtypes.html#float.hex" title="float.hex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">float.hex()</span></code></a> method.</p>
  1034. </div>
  1035. </dd></dl>
  1036. <dl class="py function">
  1037. <dt class="sig sig-object py" id="id">
  1038. <span class="sig-name descname"><span class="pre">id</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="#id" title="Link to this definition">¶</a></dt>
  1039. <dd><p>Return the “identity” of an object. This is an integer which
  1040. is guaranteed to be unique and constant for this object during its lifetime.
  1041. Two objects with non-overlapping lifetimes may have the same <a class="reference internal" href="#id" title="id"><code class="xref py py-func docutils literal notranslate"><span class="pre">id()</span></code></a>
  1042. value.</p>
  1043. <div class="impl-detail compound">
  1044. <p><strong>CPython implementation detail:</strong> This is the address of the object in memory.</p>
  1045. </div>
  1046. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">builtins.id</span></code> with argument <code class="docutils literal notranslate"><span class="pre">id</span></code>.</p>
  1047. </dd></dl>
  1048. <dl class="py function">
  1049. <dt class="sig sig-object py" id="input">
  1050. <span class="sig-name descname"><span class="pre">input</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#input" title="Link to this definition">¶</a></dt>
  1051. <dt class="sig sig-object py">
  1052. <span class="sig-name descname"><span class="pre">input</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">prompt</span></span></em><span class="sig-paren">)</span></dt>
  1053. <dd><p>If the <em>prompt</em> argument is present, it is written to standard output without
  1054. a trailing newline. The function then reads a line from input, converts it
  1055. to a string (stripping a trailing newline), and returns that. When EOF is
  1056. read, <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a> is raised. Example:</p>
  1057. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">&#39;--&gt; &#39;</span><span class="p">)</span>
  1058. <span class="go">--&gt; Monty Python&#39;s Flying Circus</span>
  1059. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span>
  1060. <span class="go">&quot;Monty Python&#39;s Flying Circus&quot;</span>
  1061. </pre></div>
  1062. </div>
  1063. <p>If the <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a> module was loaded, then <a class="reference internal" href="#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a> will use it
  1064. to provide elaborate line editing and history features.</p>
  1065. <p class="audit-hook"><p>Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">builtins.input</span></code> with
  1066. argument <code class="docutils literal notranslate"><span class="pre">prompt</span></code> before reading input</p>
  1067. </p>
  1068. <p class="audit-hook"><p>Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">builtins.input/result</span></code>
  1069. with the result after successfully reading input.</p>
  1070. </p>
  1071. </dd></dl>
  1072. <dl class="py class">
  1073. <dt class="sig sig-object py" id="int">
  1074. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">int</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#int" title="Link to this definition">¶</a></dt>
  1075. <dt class="sig sig-object py">
  1076. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">int</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">base</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">10</span></span></em><span class="sig-paren">)</span></dt>
  1077. <dd><p>Return an integer object constructed from a number or string <em>x</em>, or return
  1078. <code class="docutils literal notranslate"><span class="pre">0</span></code> if no arguments are given. If <em>x</em> defines <a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__int__()</span></code></a>,
  1079. <code class="docutils literal notranslate"><span class="pre">int(x)</span></code> returns <code class="docutils literal notranslate"><span class="pre">x.__int__()</span></code>. If <em>x</em> defines <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a>,
  1080. it returns <code class="docutils literal notranslate"><span class="pre">x.__index__()</span></code>. If <em>x</em> defines <a class="reference internal" href="../reference/datamodel.html#object.__trunc__" title="object.__trunc__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__trunc__()</span></code></a>,
  1081. it returns <code class="docutils literal notranslate"><span class="pre">x.__trunc__()</span></code>.
  1082. For floating point numbers, this truncates towards zero.</p>
  1083. <p>If <em>x</em> is not a number or if <em>base</em> is given, then <em>x</em> must be a string,
  1084. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>, or <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> instance representing an integer
  1085. in radix <em>base</em>. Optionally, the string can be preceded by <code class="docutils literal notranslate"><span class="pre">+</span></code> or <code class="docutils literal notranslate"><span class="pre">-</span></code>
  1086. (with no space in between), have leading zeros, be surrounded by whitespace,
  1087. and have single underscores interspersed between digits.</p>
  1088. <p>A base-n integer string contains digits, each representing a value from 0 to
  1089. n-1. The values 0–9 can be represented by any Unicode decimal digit. The
  1090. values 10–35 can be represented by <code class="docutils literal notranslate"><span class="pre">a</span></code> to <code class="docutils literal notranslate"><span class="pre">z</span></code> (or <code class="docutils literal notranslate"><span class="pre">A</span></code> to <code class="docutils literal notranslate"><span class="pre">Z</span></code>). The
  1091. default <em>base</em> is 10. The allowed bases are 0 and 2–36. Base-2, -8, and -16
  1092. strings can be optionally prefixed with <code class="docutils literal notranslate"><span class="pre">0b</span></code>/<code class="docutils literal notranslate"><span class="pre">0B</span></code>, <code class="docutils literal notranslate"><span class="pre">0o</span></code>/<code class="docutils literal notranslate"><span class="pre">0O</span></code>, or
  1093. <code class="docutils literal notranslate"><span class="pre">0x</span></code>/<code class="docutils literal notranslate"><span class="pre">0X</span></code>, as with integer literals in code. For base 0, the string is
  1094. interpreted in a similar way to an <a class="reference internal" href="../reference/lexical_analysis.html#integers"><span class="std std-ref">integer literal in code</span></a>,
  1095. in that the actual base is 2, 8, 10, or 16 as determined by the prefix. Base
  1096. 0 also disallows leading zeros: <code class="docutils literal notranslate"><span class="pre">int('010',</span> <span class="pre">0)</span></code> is not legal, while
  1097. <code class="docutils literal notranslate"><span class="pre">int('010')</span></code> and <code class="docutils literal notranslate"><span class="pre">int('010',</span> <span class="pre">8)</span></code> are.</p>
  1098. <p>The integer type is described in <a class="reference internal" href="stdtypes.html#typesnumeric"><span class="std std-ref">Numeric Types — int, float, complex</span></a>.</p>
  1099. <div class="versionchanged">
  1100. <p><span class="versionmodified changed">Changed in version 3.4: </span>If <em>base</em> is not an instance of <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> and the <em>base</em> object has a
  1101. <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">base.__index__</span></code></a> method, that method is called
  1102. to obtain an integer for the base. Previous versions used
  1103. <a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">base.__int__</span></code></a> instead of <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">base.__index__</span></code></a>.</p>
  1104. </div>
  1105. <div class="versionchanged">
  1106. <p><span class="versionmodified changed">Changed in version 3.6: </span>Grouping digits with underscores as in code literals is allowed.</p>
  1107. </div>
  1108. <div class="versionchanged">
  1109. <p><span class="versionmodified changed">Changed in version 3.7: </span><em>x</em> is now a positional-only parameter.</p>
  1110. </div>
  1111. <div class="versionchanged">
  1112. <p><span class="versionmodified changed">Changed in version 3.8: </span>Falls back to <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> if <a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__int__()</span></code></a> is not defined.</p>
  1113. </div>
  1114. <div class="versionchanged">
  1115. <p><span class="versionmodified changed">Changed in version 3.11: </span>The delegation to <a class="reference internal" href="../reference/datamodel.html#object.__trunc__" title="object.__trunc__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__trunc__()</span></code></a> is deprecated.</p>
  1116. </div>
  1117. <div class="versionchanged">
  1118. <p><span class="versionmodified changed">Changed in version 3.11: </span><a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> string inputs and string representations can be limited to
  1119. help avoid denial of service attacks. A <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised when
  1120. the limit is exceeded while converting a string <em>x</em> to an <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> or
  1121. when converting an <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> into a string would exceed the limit.
  1122. See the <a class="reference internal" href="stdtypes.html#int-max-str-digits"><span class="std std-ref">integer string conversion length limitation</span></a> documentation.</p>
  1123. </div>
  1124. </dd></dl>
  1125. <dl class="py function">
  1126. <dt class="sig sig-object py" id="isinstance">
  1127. <span class="sig-name descname"><span class="pre">isinstance</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">classinfo</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#isinstance" title="Link to this definition">¶</a></dt>
  1128. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the <em>object</em> argument is an instance of the <em>classinfo</em>
  1129. argument, or of a (direct, indirect, or <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">virtual</span></a>) subclass thereof. If <em>object</em> is not
  1130. an object of the given type, the function always returns <code class="docutils literal notranslate"><span class="pre">False</span></code>.
  1131. If <em>classinfo</em> is a tuple of type objects (or recursively, other such
  1132. tuples) or a <a class="reference internal" href="stdtypes.html#types-union"><span class="std std-ref">Union Type</span></a> of multiple types, return <code class="docutils literal notranslate"><span class="pre">True</span></code> if
  1133. <em>object</em> is an instance of any of the types.
  1134. If <em>classinfo</em> is not a type or tuple of types and such tuples,
  1135. a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception is raised. <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> may not be
  1136. raised for an invalid type if an earlier check succeeds.</p>
  1137. <div class="versionchanged">
  1138. <p><span class="versionmodified changed">Changed in version 3.10: </span><em>classinfo</em> can be a <a class="reference internal" href="stdtypes.html#types-union"><span class="std std-ref">Union Type</span></a>.</p>
  1139. </div>
  1140. </dd></dl>
  1141. <dl class="py function">
  1142. <dt class="sig sig-object py" id="issubclass">
  1143. <span class="sig-name descname"><span class="pre">issubclass</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">class</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">classinfo</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#issubclass" title="Link to this definition">¶</a></dt>
  1144. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>class</em> is a subclass (direct, indirect, or <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">virtual</span></a>) of <em>classinfo</em>. A
  1145. class is considered a subclass of itself. <em>classinfo</em> may be a tuple of class
  1146. objects (or recursively, other such tuples)
  1147. or a <a class="reference internal" href="stdtypes.html#types-union"><span class="std std-ref">Union Type</span></a>, in which case return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>class</em> is a
  1148. subclass of any entry in <em>classinfo</em>. In any other case, a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>
  1149. exception is raised.</p>
  1150. <div class="versionchanged">
  1151. <p><span class="versionmodified changed">Changed in version 3.10: </span><em>classinfo</em> can be a <a class="reference internal" href="stdtypes.html#types-union"><span class="std std-ref">Union Type</span></a>.</p>
  1152. </div>
  1153. </dd></dl>
  1154. <dl class="py function">
  1155. <dt class="sig sig-object py" id="iter">
  1156. <span class="sig-name descname"><span class="pre">iter</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="#iter" title="Link to this definition">¶</a></dt>
  1157. <dt class="sig sig-object py">
  1158. <span class="sig-name descname"><span class="pre">iter</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">sentinel</span></span></em><span class="sig-paren">)</span></dt>
  1159. <dd><p>Return an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> object. The first argument is interpreted very
  1160. differently depending on the presence of the second argument. Without a
  1161. second argument, <em>object</em> must be a collection object which supports the
  1162. <a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a> protocol (the <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code></a> method),
  1163. or it must support
  1164. the sequence protocol (the <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> method with integer arguments
  1165. starting at <code class="docutils literal notranslate"><span class="pre">0</span></code>). If it does not support either of those protocols,
  1166. <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised. If the second argument, <em>sentinel</em>, is given,
  1167. then <em>object</em> must be a callable object. The iterator created in this case
  1168. will call <em>object</em> with no arguments for each call to its
  1169. <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> method; if the value returned is equal to
  1170. <em>sentinel</em>, <a class="reference internal" href="exceptions.html#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> will be raised, otherwise the value will
  1171. be returned.</p>
  1172. <p>See also <a class="reference internal" href="stdtypes.html#typeiter"><span class="std std-ref">Iterator Types</span></a>.</p>
  1173. <p>One useful application of the second form of <a class="reference internal" href="#iter" title="iter"><code class="xref py py-func docutils literal notranslate"><span class="pre">iter()</span></code></a> is to build a
  1174. block-reader. For example, reading fixed-width blocks from a binary
  1175. database file until the end of file is reached:</p>
  1176. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">partial</span>
  1177. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;mydata.db&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1178. <span class="k">for</span> <span class="n">block</span> <span class="ow">in</span> <span class="nb">iter</span><span class="p">(</span><span class="n">partial</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">,</span> <span class="mi">64</span><span class="p">),</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
  1179. <span class="n">process_block</span><span class="p">(</span><span class="n">block</span><span class="p">)</span>
  1180. </pre></div>
  1181. </div>
  1182. </dd></dl>
  1183. <dl class="py function">
  1184. <dt class="sig sig-object py" id="len">
  1185. <span class="sig-name descname"><span class="pre">len</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#len" title="Link to this definition">¶</a></dt>
  1186. <dd><p>Return the length (the number of items) of an object. The argument may be a
  1187. sequence (such as a string, bytes, tuple, list, or range) or a collection
  1188. (such as a dictionary, set, or frozen set).</p>
  1189. <div class="impl-detail compound">
  1190. <p><strong>CPython implementation detail:</strong> <code class="docutils literal notranslate"><span class="pre">len</span></code> raises <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> on lengths larger than
  1191. <a class="reference internal" href="sys.html#sys.maxsize" title="sys.maxsize"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxsize</span></code></a>, such as <a class="reference internal" href="stdtypes.html#range" title="range"><code class="xref py py-class docutils literal notranslate"><span class="pre">range(2</span> <span class="pre">**</span> <span class="pre">100)</span></code></a>.</p>
  1192. </div>
  1193. </dd></dl>
  1194. <dl class="py class" id="func-list">
  1195. <dt class="sig sig-object py">
  1196. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">list</span></span></dt>
  1197. <dt class="sig sig-object py">
  1198. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">list</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="sig-paren">)</span></dt>
  1199. <dd><p>Rather than being a function, <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> is actually a mutable
  1200. sequence type, as documented in <a class="reference internal" href="stdtypes.html#typesseq-list"><span class="std std-ref">Lists</span></a> and <a class="reference internal" href="stdtypes.html#typesseq"><span class="std std-ref">Sequence Types — list, tuple, range</span></a>.</p>
  1201. </dd></dl>
  1202. <dl class="py function">
  1203. <dt class="sig sig-object py" id="locals">
  1204. <span class="sig-name descname"><span class="pre">locals</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#locals" title="Link to this definition">¶</a></dt>
  1205. <dd><p>Update and return a dictionary representing the current local symbol table.
  1206. Free variables are returned by <a class="reference internal" href="#locals" title="locals"><code class="xref py py-func docutils literal notranslate"><span class="pre">locals()</span></code></a> when it is called in function
  1207. blocks, but not in class blocks. Note that at the module level, <a class="reference internal" href="#locals" title="locals"><code class="xref py py-func docutils literal notranslate"><span class="pre">locals()</span></code></a>
  1208. and <a class="reference internal" href="#globals" title="globals"><code class="xref py py-func docutils literal notranslate"><span class="pre">globals()</span></code></a> are the same dictionary.</p>
  1209. <div class="admonition note">
  1210. <p class="admonition-title">Note</p>
  1211. <p>The contents of this dictionary should not be modified; changes may not
  1212. affect the values of local and free variables used by the interpreter.</p>
  1213. </div>
  1214. </dd></dl>
  1215. <dl class="py function">
  1216. <dt class="sig sig-object py" id="map">
  1217. <span class="sig-name descname"><span class="pre">map</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">function</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">iterables</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#map" title="Link to this definition">¶</a></dt>
  1218. <dd><p>Return an iterator that applies <em>function</em> to every item of <em>iterable</em>,
  1219. yielding the results. If additional <em>iterables</em> arguments are passed,
  1220. <em>function</em> must take that many arguments and is applied to the items from all
  1221. iterables in parallel. With multiple iterables, the iterator stops when the
  1222. shortest iterable is exhausted. For cases where the function inputs are
  1223. already arranged into argument tuples, see <a class="reference internal" href="itertools.html#itertools.starmap" title="itertools.starmap"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.starmap()</span></code></a>.</p>
  1224. </dd></dl>
  1225. <dl class="py function">
  1226. <dt class="sig sig-object py" id="max">
  1227. <span class="sig-name descname"><span class="pre">max</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</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">key</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#max" title="Link to this definition">¶</a></dt>
  1228. <dt class="sig sig-object py">
  1229. <span class="sig-name descname"><span class="pre">max</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</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">default</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">key</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span></dt>
  1230. <dt class="sig sig-object py">
  1231. <span class="sig-name descname"><span class="pre">max</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arg2</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">key</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span></dt>
  1232. <dd><p>Return the largest item in an iterable or the largest of two or more
  1233. arguments.</p>
  1234. <p>If one positional argument is provided, it should be an <a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a>.
  1235. The largest item in the iterable is returned. If two or more positional
  1236. arguments are provided, the largest of the positional arguments is
  1237. returned.</p>
  1238. <p>There are two optional keyword-only arguments. The <em>key</em> argument specifies
  1239. a one-argument ordering function like that used for <a class="reference internal" href="stdtypes.html#list.sort" title="list.sort"><code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code></a>. The
  1240. <em>default</em> argument specifies an object to return if the provided iterable is
  1241. empty. If the iterable is empty and <em>default</em> is not provided, a
  1242. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
  1243. <p>If multiple items are maximal, the function returns the first one
  1244. encountered. This is consistent with other sort-stability preserving tools
  1245. such as <code class="docutils literal notranslate"><span class="pre">sorted(iterable,</span> <span class="pre">key=keyfunc,</span> <span class="pre">reverse=True)[0]</span></code> and
  1246. <code class="docutils literal notranslate"><span class="pre">heapq.nlargest(1,</span> <span class="pre">iterable,</span> <span class="pre">key=keyfunc)</span></code>.</p>
  1247. <div class="versionchanged">
  1248. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <em>default</em> keyword-only parameter.</p>
  1249. </div>
  1250. <div class="versionchanged">
  1251. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <em>key</em> can be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1252. </div>
  1253. </dd></dl>
  1254. <dl class="py class" id="func-memoryview">
  1255. <dt class="sig sig-object py">
  1256. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">memoryview</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></dt>
  1257. <dd><p>Return a “memory view” object created from the given argument. See
  1258. <a class="reference internal" href="stdtypes.html#typememoryview"><span class="std std-ref">Memory Views</span></a> for more information.</p>
  1259. </dd></dl>
  1260. <dl class="py function">
  1261. <dt class="sig sig-object py" id="min">
  1262. <span class="sig-name descname"><span class="pre">min</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</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">key</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#min" title="Link to this definition">¶</a></dt>
  1263. <dt class="sig sig-object py">
  1264. <span class="sig-name descname"><span class="pre">min</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</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">default</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">key</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span></dt>
  1265. <dt class="sig sig-object py">
  1266. <span class="sig-name descname"><span class="pre">min</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arg2</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">key</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span></dt>
  1267. <dd><p>Return the smallest item in an iterable or the smallest of two or more
  1268. arguments.</p>
  1269. <p>If one positional argument is provided, it should be an <a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a>.
  1270. The smallest item in the iterable is returned. If two or more positional
  1271. arguments are provided, the smallest of the positional arguments is
  1272. returned.</p>
  1273. <p>There are two optional keyword-only arguments. The <em>key</em> argument specifies
  1274. a one-argument ordering function like that used for <a class="reference internal" href="stdtypes.html#list.sort" title="list.sort"><code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code></a>. The
  1275. <em>default</em> argument specifies an object to return if the provided iterable is
  1276. empty. If the iterable is empty and <em>default</em> is not provided, a
  1277. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
  1278. <p>If multiple items are minimal, the function returns the first one
  1279. encountered. This is consistent with other sort-stability preserving tools
  1280. such as <code class="docutils literal notranslate"><span class="pre">sorted(iterable,</span> <span class="pre">key=keyfunc)[0]</span></code> and <code class="docutils literal notranslate"><span class="pre">heapq.nsmallest(1,</span>
  1281. <span class="pre">iterable,</span> <span class="pre">key=keyfunc)</span></code>.</p>
  1282. <div class="versionchanged">
  1283. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <em>default</em> keyword-only parameter.</p>
  1284. </div>
  1285. <div class="versionchanged">
  1286. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <em>key</em> can be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1287. </div>
  1288. </dd></dl>
  1289. <dl class="py function">
  1290. <dt class="sig sig-object py" id="next">
  1291. <span class="sig-name descname"><span class="pre">next</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterator</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#next" title="Link to this definition">¶</a></dt>
  1292. <dt class="sig sig-object py">
  1293. <span class="sig-name descname"><span class="pre">next</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterator</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span></em><span class="sig-paren">)</span></dt>
  1294. <dd><p>Retrieve the next item from the <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> by calling its
  1295. <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> method. If <em>default</em> is given, it is returned
  1296. if the iterator is exhausted, otherwise <a class="reference internal" href="exceptions.html#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> is raised.</p>
  1297. </dd></dl>
  1298. <dl class="py class">
  1299. <dt class="sig sig-object py" id="object">
  1300. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">object</span></span><a class="headerlink" href="#object" title="Link to this definition">¶</a></dt>
  1301. <dd><p>Return a new featureless object. <a class="reference internal" href="#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a> is a base for all classes.
  1302. It has methods that are common to all instances of Python classes. This
  1303. function does not accept any arguments.</p>
  1304. <div class="admonition note">
  1305. <p class="admonition-title">Note</p>
  1306. <p><a class="reference internal" href="#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a> does <em>not</em> have a <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a>, so you can’t
  1307. assign arbitrary attributes to an instance of the <a class="reference internal" href="#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a> class.</p>
  1308. </div>
  1309. </dd></dl>
  1310. <dl class="py function">
  1311. <dt class="sig sig-object py" id="oct">
  1312. <span class="sig-name descname"><span class="pre">oct</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#oct" title="Link to this definition">¶</a></dt>
  1313. <dd><p>Convert an integer number to an octal string prefixed with “0o”. The result
  1314. is a valid Python expression. If <em>x</em> is not a Python <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> object, it
  1315. has to define an <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> method that returns an integer. For
  1316. example:</p>
  1317. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">oct</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span>
  1318. <span class="go">&#39;0o10&#39;</span>
  1319. <span class="gp">&gt;&gt;&gt; </span><span class="nb">oct</span><span class="p">(</span><span class="o">-</span><span class="mi">56</span><span class="p">)</span>
  1320. <span class="go">&#39;-0o70&#39;</span>
  1321. </pre></div>
  1322. </div>
  1323. <p>If you want to convert an integer number to an octal string either with the prefix
  1324. “0o” or not, you can use either of the following ways.</p>
  1325. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">%#o</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="mi">10</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%o</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="mi">10</span>
  1326. <span class="go">(&#39;0o12&#39;, &#39;12&#39;)</span>
  1327. <span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="s1">&#39;#o&#39;</span><span class="p">),</span> <span class="nb">format</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="s1">&#39;o&#39;</span><span class="p">)</span>
  1328. <span class="go">(&#39;0o12&#39;, &#39;12&#39;)</span>
  1329. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="mi">10</span><span class="si">:</span><span class="s1">#o</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="mi">10</span><span class="si">:</span><span class="s1">o</span><span class="si">}</span><span class="s1">&#39;</span>
  1330. <span class="go">(&#39;0o12&#39;, &#39;12&#39;)</span>
  1331. </pre></div>
  1332. </div>
  1333. <p>See also <a class="reference internal" href="#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> for more information.</p>
  1334. </dd></dl>
  1335. <dl class="py function" id="index-5">
  1336. <dt class="sig sig-object py" id="open">
  1337. <span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffering</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">encoding</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">errors</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">newline</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">closefd</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">opener</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#open" title="Link to this definition">¶</a></dt>
  1338. <dd><p>Open <em>file</em> and return a corresponding <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>. If the file
  1339. cannot be opened, an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised. See
  1340. <a class="reference internal" href="../tutorial/inputoutput.html#tut-files"><span class="std std-ref">Reading and Writing Files</span></a> for more examples of how to use this function.</p>
  1341. <p><em>file</em> is a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> giving the pathname (absolute or
  1342. relative to the current working directory) of the file to be opened or an
  1343. integer file descriptor of the file to be wrapped. (If a file descriptor is
  1344. given, it is closed when the returned I/O object is closed unless <em>closefd</em>
  1345. is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>.)</p>
  1346. <p><em>mode</em> is an optional string that specifies the mode in which the file is
  1347. opened. It defaults to <code class="docutils literal notranslate"><span class="pre">'r'</span></code> which means open for reading in text mode.
  1348. Other common values are <code class="docutils literal notranslate"><span class="pre">'w'</span></code> for writing (truncating the file if it
  1349. already exists), <code class="docutils literal notranslate"><span class="pre">'x'</span></code> for exclusive creation, and <code class="docutils literal notranslate"><span class="pre">'a'</span></code> for appending
  1350. (which on <em>some</em> Unix systems, means that <em>all</em> writes append to the end of
  1351. the file regardless of the current seek position). In text mode, if
  1352. <em>encoding</em> is not specified the encoding used is platform-dependent:
  1353. <a class="reference internal" href="locale.html#locale.getencoding" title="locale.getencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getencoding()</span></code></a> is called to get the current locale encoding.
  1354. (For reading and writing raw bytes use binary mode and leave
  1355. <em>encoding</em> unspecified.) The available modes are:</p>
  1356. <span id="filemodes"></span><table class="docutils align-default" id="index-6">
  1357. <thead>
  1358. <tr class="row-odd"><th class="head"><p>Character</p></th>
  1359. <th class="head"><p>Meaning</p></th>
  1360. </tr>
  1361. </thead>
  1362. <tbody>
  1363. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'r'</span></code></p></td>
  1364. <td><p>open for reading (default)</p></td>
  1365. </tr>
  1366. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'w'</span></code></p></td>
  1367. <td><p>open for writing, truncating the file first</p></td>
  1368. </tr>
  1369. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'x'</span></code></p></td>
  1370. <td><p>open for exclusive creation, failing if the file already exists</p></td>
  1371. </tr>
  1372. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'a'</span></code></p></td>
  1373. <td><p>open for writing, appending to the end of file if it exists</p></td>
  1374. </tr>
  1375. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'b'</span></code></p></td>
  1376. <td><p>binary mode</p></td>
  1377. </tr>
  1378. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'t'</span></code></p></td>
  1379. <td><p>text mode (default)</p></td>
  1380. </tr>
  1381. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'+'</span></code></p></td>
  1382. <td><p>open for updating (reading and writing)</p></td>
  1383. </tr>
  1384. </tbody>
  1385. </table>
  1386. <p>The default mode is <code class="docutils literal notranslate"><span class="pre">'r'</span></code> (open for reading text, a synonym of <code class="docutils literal notranslate"><span class="pre">'rt'</span></code>).
  1387. Modes <code class="docutils literal notranslate"><span class="pre">'w+'</span></code> and <code class="docutils literal notranslate"><span class="pre">'w+b'</span></code> open and truncate the file. Modes <code class="docutils literal notranslate"><span class="pre">'r+'</span></code>
  1388. and <code class="docutils literal notranslate"><span class="pre">'r+b'</span></code> open the file with no truncation.</p>
  1389. <p>As mentioned in the <a class="reference internal" href="io.html#io-overview"><span class="std std-ref">Overview</span></a>, Python distinguishes between binary
  1390. and text I/O. Files opened in binary mode (including <code class="docutils literal notranslate"><span class="pre">'b'</span></code> in the <em>mode</em>
  1391. argument) return contents as <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> objects without any decoding. In
  1392. text mode (the default, or when <code class="docutils literal notranslate"><span class="pre">'t'</span></code> is included in the <em>mode</em> argument),
  1393. the contents of the file are returned as <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, the bytes having been
  1394. first decoded using a platform-dependent encoding or using the specified
  1395. <em>encoding</em> if given.</p>
  1396. <div class="admonition note">
  1397. <p class="admonition-title">Note</p>
  1398. <p>Python doesn’t depend on the underlying operating system’s notion of text
  1399. files; all the processing is done by Python itself, and is therefore
  1400. platform-independent.</p>
  1401. </div>
  1402. <p><em>buffering</em> is an optional integer used to set the buffering policy. Pass 0
  1403. to switch buffering off (only allowed in binary mode), 1 to select line
  1404. buffering (only usable when writing in text mode), and an integer &gt; 1 to indicate the size
  1405. in bytes of a fixed-size chunk buffer. Note that specifying a buffer size this
  1406. way applies for binary buffered I/O, but <code class="docutils literal notranslate"><span class="pre">TextIOWrapper</span></code> (i.e., files opened
  1407. with <code class="docutils literal notranslate"><span class="pre">mode='r+'</span></code>) would have another buffering. To disable buffering in
  1408. <code class="docutils literal notranslate"><span class="pre">TextIOWrapper</span></code>, consider using the <code class="docutils literal notranslate"><span class="pre">write_through</span></code> flag for
  1409. <a class="reference internal" href="io.html#io.TextIOWrapper.reconfigure" title="io.TextIOWrapper.reconfigure"><code class="xref py py-func docutils literal notranslate"><span class="pre">io.TextIOWrapper.reconfigure()</span></code></a>. When no <em>buffering</em> argument is
  1410. given, the default buffering policy works as follows:</p>
  1411. <ul class="simple">
  1412. <li><p>Binary files are buffered in fixed-size chunks; the size of the buffer is
  1413. chosen using a heuristic trying to determine the underlying device’s “block
  1414. size” and falling back on <a class="reference internal" href="io.html#io.DEFAULT_BUFFER_SIZE" title="io.DEFAULT_BUFFER_SIZE"><code class="xref py py-const docutils literal notranslate"><span class="pre">io.DEFAULT_BUFFER_SIZE</span></code></a>. On many systems,
  1415. the buffer will typically be 4096 or 8192 bytes long.</p></li>
  1416. <li><p>“Interactive” text files (files for which <a class="reference internal" href="io.html#io.IOBase.isatty" title="io.IOBase.isatty"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isatty()</span></code></a>
  1417. returns <code class="docutils literal notranslate"><span class="pre">True</span></code>) use line buffering. Other text files use the policy
  1418. described above for binary files.</p></li>
  1419. </ul>
  1420. <p><em>encoding</em> is the name of the encoding used to decode or encode the file.
  1421. This should only be used in text mode. The default encoding is platform
  1422. dependent (whatever <a class="reference internal" href="locale.html#locale.getencoding" title="locale.getencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getencoding()</span></code></a> returns), but any
  1423. <a class="reference internal" href="../glossary.html#term-text-encoding"><span class="xref std std-term">text encoding</span></a> supported by Python can be used.
  1424. See the <a class="reference internal" href="codecs.html#module-codecs" title="codecs: Encode and decode data and streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code></a> module for the list of supported encodings.</p>
  1425. <p><em>errors</em> is an optional string that specifies how encoding and decoding
  1426. errors are to be handled—this cannot be used in binary mode.
  1427. A variety of standard error handlers are available
  1428. (listed under <a class="reference internal" href="codecs.html#error-handlers"><span class="std std-ref">Error Handlers</span></a>), though any
  1429. error handling name that has been registered with
  1430. <a class="reference internal" href="codecs.html#codecs.register_error" title="codecs.register_error"><code class="xref py py-func docutils literal notranslate"><span class="pre">codecs.register_error()</span></code></a> is also valid. The standard names
  1431. include:</p>
  1432. <ul class="simple">
  1433. <li><p><code class="docutils literal notranslate"><span class="pre">'strict'</span></code> to raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> exception if there is
  1434. an encoding error. The default value of <code class="docutils literal notranslate"><span class="pre">None</span></code> has the same
  1435. effect.</p></li>
  1436. <li><p><code class="docutils literal notranslate"><span class="pre">'ignore'</span></code> ignores errors. Note that ignoring encoding errors
  1437. can lead to data loss.</p></li>
  1438. <li><p><code class="docutils literal notranslate"><span class="pre">'replace'</span></code> causes a replacement marker (such as <code class="docutils literal notranslate"><span class="pre">'?'</span></code>) to be inserted
  1439. where there is malformed data.</p></li>
  1440. <li><p><code class="docutils literal notranslate"><span class="pre">'surrogateescape'</span></code> will represent any incorrect bytes as low
  1441. surrogate code units ranging from U+DC80 to U+DCFF.
  1442. These surrogate code units will then be turned back into
  1443. the same bytes when the <code class="docutils literal notranslate"><span class="pre">surrogateescape</span></code> error handler is used
  1444. when writing data. This is useful for processing files in an
  1445. unknown encoding.</p></li>
  1446. <li><p><code class="docutils literal notranslate"><span class="pre">'xmlcharrefreplace'</span></code> is only supported when writing to a file.
  1447. Characters not supported by the encoding are replaced with the
  1448. appropriate XML character reference <code class="samp docutils literal notranslate"><span class="pre">&amp;#</span><em><span class="pre">nnn</span></em><span class="pre">;</span></code>.</p></li>
  1449. <li><p><code class="docutils literal notranslate"><span class="pre">'backslashreplace'</span></code> replaces malformed data by Python’s backslashed
  1450. escape sequences.</p></li>
  1451. <li><p><code class="docutils literal notranslate"><span class="pre">'namereplace'</span></code> (also only supported when writing)
  1452. replaces unsupported characters with <code class="docutils literal notranslate"><span class="pre">\N{...}</span></code> escape sequences.</p></li>
  1453. </ul>
  1454. <p id="open-newline-parameter"><span id="index-7"></span><em>newline</em> determines how to parse newline characters from the stream.
  1455. It can be <code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="docutils literal notranslate"><span class="pre">''</span></code>, <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>, <code class="docutils literal notranslate"><span class="pre">'\r'</span></code>, and
  1456. <code class="docutils literal notranslate"><span class="pre">'\r\n'</span></code>. It works as follows:</p>
  1457. <ul class="simple">
  1458. <li><p>When reading input from the stream, if <em>newline</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, universal
  1459. newlines mode is enabled. Lines in the input can end in <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>,
  1460. <code class="docutils literal notranslate"><span class="pre">'\r'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'\r\n'</span></code>, and these are translated into <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> before
  1461. being returned to the caller. If it is <code class="docutils literal notranslate"><span class="pre">''</span></code>, universal newlines mode is
  1462. enabled, but line endings are returned to the caller untranslated. If it
  1463. has any of the other legal values, input lines are only terminated by the
  1464. given string, and the line ending is returned to the caller untranslated.</p></li>
  1465. <li><p>When writing output to the stream, if <em>newline</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, any <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>
  1466. characters written are translated to the system default line separator,
  1467. <a class="reference internal" href="os.html#os.linesep" title="os.linesep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.linesep</span></code></a>. If <em>newline</em> is <code class="docutils literal notranslate"><span class="pre">''</span></code> or <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>, no translation
  1468. takes place. If <em>newline</em> is any of the other legal values, any <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>
  1469. characters written are translated to the given string.</p></li>
  1470. </ul>
  1471. <p>If <em>closefd</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> and a file descriptor rather than a filename was
  1472. given, the underlying file descriptor will be kept open when the file is
  1473. closed. If a filename is given <em>closefd</em> must be <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default);
  1474. otherwise, an error will be raised.</p>
  1475. <p>A custom opener can be used by passing a callable as <em>opener</em>. The underlying
  1476. file descriptor for the file object is then obtained by calling <em>opener</em> with
  1477. (<em>file</em>, <em>flags</em>). <em>opener</em> must return an open file descriptor (passing
  1478. <a class="reference internal" href="os.html#os.open" title="os.open"><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.open</span></code></a> as <em>opener</em> results in functionality similar to passing
  1479. <code class="docutils literal notranslate"><span class="pre">None</span></code>).</p>
  1480. <p>The newly created file is <a class="reference internal" href="os.html#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p>
  1481. <p>The following example uses the <a class="reference internal" href="os.html#dir-fd"><span class="std std-ref">dir_fd</span></a> parameter of the
  1482. <a class="reference internal" href="os.html#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a> function to open a file relative to a given directory:</p>
  1483. <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">os</span>
  1484. <span class="gp">&gt;&gt;&gt; </span><span class="n">dir_fd</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;somedir&#39;</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">O_RDONLY</span><span class="p">)</span>
  1485. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">opener</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">flags</span><span class="p">):</span>
  1486. <span class="gp">... </span> <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">flags</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="n">dir_fd</span><span class="p">)</span>
  1487. <span class="gp">...</span>
  1488. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;spamspam.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">opener</span><span class="o">=</span><span class="n">opener</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1489. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;This will be written to somedir/spamspam.txt&#39;</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">f</span><span class="p">)</span>
  1490. <span class="gp">...</span>
  1491. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">dir_fd</span><span class="p">)</span> <span class="c1"># don&#39;t leak a file descriptor</span>
  1492. </pre></div>
  1493. </div>
  1494. <p>The type of <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> returned by the <a class="reference internal" href="#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function
  1495. depends on the mode. When <a class="reference internal" href="#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> is used to open a file in a text
  1496. mode (<code class="docutils literal notranslate"><span class="pre">'w'</span></code>, <code class="docutils literal notranslate"><span class="pre">'r'</span></code>, <code class="docutils literal notranslate"><span class="pre">'wt'</span></code>, <code class="docutils literal notranslate"><span class="pre">'rt'</span></code>, etc.), it returns a subclass of
  1497. <a class="reference internal" href="io.html#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOBase</span></code></a> (specifically <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a>). When used
  1498. to open a file in a binary mode with buffering, the returned class is a
  1499. subclass of <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedIOBase</span></code></a>. The exact class varies: in read
  1500. binary mode, it returns an <a class="reference internal" href="io.html#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedReader</span></code></a>; in write binary and
  1501. append binary modes, it returns an <a class="reference internal" href="io.html#io.BufferedWriter" title="io.BufferedWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedWriter</span></code></a>, and in
  1502. read/write mode, it returns an <a class="reference internal" href="io.html#io.BufferedRandom" title="io.BufferedRandom"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedRandom</span></code></a>. When buffering is
  1503. disabled, the raw stream, a subclass of <a class="reference internal" href="io.html#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.RawIOBase</span></code></a>,
  1504. <a class="reference internal" href="io.html#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.FileIO</span></code></a>, is returned.</p>
  1505. <p id="index-8">See also the file handling modules, such as <a class="reference internal" href="fileinput.html#module-fileinput" title="fileinput: Loop over standard input or a list of files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">fileinput</span></code></a>, <a class="reference internal" href="io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a>
  1506. (where <a class="reference internal" href="#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> is declared), <a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a>, <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a>, <a class="reference internal" href="tempfile.html#module-tempfile" title="tempfile: Generate temporary files and directories."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code></a>,
  1507. and <a class="reference internal" href="shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a>.</p>
  1508. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">open</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">file</span></code>, <code class="docutils literal notranslate"><span class="pre">mode</span></code>, <code class="docutils literal notranslate"><span class="pre">flags</span></code>.</p>
  1509. <p>The <code class="docutils literal notranslate"><span class="pre">mode</span></code> and <code class="docutils literal notranslate"><span class="pre">flags</span></code> arguments may have been modified or inferred from
  1510. the original call.</p>
  1511. <div class="versionchanged">
  1512. <p><span class="versionmodified changed">Changed in version 3.3: </span></p>
  1513. <ul class="simple">
  1514. <li><p>The <em>opener</em> parameter was added.</p></li>
  1515. <li><p>The <code class="docutils literal notranslate"><span class="pre">'x'</span></code> mode was added.</p></li>
  1516. <li><p><a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a> used to be raised, it is now an alias of <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>.</p></li>
  1517. <li><p><a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> is now raised if the file opened in exclusive
  1518. creation mode (<code class="docutils literal notranslate"><span class="pre">'x'</span></code>) already exists.</p></li>
  1519. </ul>
  1520. </div>
  1521. <div class="versionchanged">
  1522. <p><span class="versionmodified changed">Changed in version 3.4: </span></p>
  1523. <ul class="simple">
  1524. <li><p>The file is now non-inheritable.</p></li>
  1525. </ul>
  1526. </div>
  1527. <div class="versionchanged">
  1528. <p><span class="versionmodified changed">Changed in version 3.5: </span></p>
  1529. <ul class="simple">
  1530. <li><p>If the system call is interrupted and the signal handler does not raise an
  1531. exception, the function now retries the system call instead of raising an
  1532. <a class="reference internal" href="exceptions.html#InterruptedError" title="InterruptedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InterruptedError</span></code></a> exception (see <span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a> for the rationale).</p></li>
  1533. <li><p>The <code class="docutils literal notranslate"><span class="pre">'namereplace'</span></code> error handler was added.</p></li>
  1534. </ul>
  1535. </div>
  1536. <div class="versionchanged">
  1537. <p><span class="versionmodified changed">Changed in version 3.6: </span></p>
  1538. <ul class="simple">
  1539. <li><p>Support added to accept objects implementing <a class="reference internal" href="os.html#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.PathLike</span></code></a>.</p></li>
  1540. <li><p>On Windows, opening a console buffer may return a subclass of
  1541. <a class="reference internal" href="io.html#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.RawIOBase</span></code></a> other than <a class="reference internal" href="io.html#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.FileIO</span></code></a>.</p></li>
  1542. </ul>
  1543. </div>
  1544. <div class="versionchanged">
  1545. <p><span class="versionmodified changed">Changed in version 3.11: </span>The <code class="docutils literal notranslate"><span class="pre">'U'</span></code> mode has been removed.</p>
  1546. </div>
  1547. </dd></dl>
  1548. <dl class="py function">
  1549. <dt class="sig sig-object py" id="ord">
  1550. <span class="sig-name descname"><span class="pre">ord</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">c</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#ord" title="Link to this definition">¶</a></dt>
  1551. <dd><p>Given a string representing one Unicode character, return an integer
  1552. representing the Unicode code point of that character. For example,
  1553. <code class="docutils literal notranslate"><span class="pre">ord('a')</span></code> returns the integer <code class="docutils literal notranslate"><span class="pre">97</span></code> and <code class="docutils literal notranslate"><span class="pre">ord('€')</span></code> (Euro sign)
  1554. returns <code class="docutils literal notranslate"><span class="pre">8364</span></code>. This is the inverse of <a class="reference internal" href="#chr" title="chr"><code class="xref py py-func docutils literal notranslate"><span class="pre">chr()</span></code></a>.</p>
  1555. </dd></dl>
  1556. <dl class="py function">
  1557. <dt class="sig sig-object py" id="pow">
  1558. <span class="sig-name descname"><span class="pre">pow</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">base</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mod</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pow" title="Link to this definition">¶</a></dt>
  1559. <dd><p>Return <em>base</em> to the power <em>exp</em>; if <em>mod</em> is present, return <em>base</em> to the
  1560. power <em>exp</em>, modulo <em>mod</em> (computed more efficiently than
  1561. <code class="docutils literal notranslate"><span class="pre">pow(base,</span> <span class="pre">exp)</span> <span class="pre">%</span> <span class="pre">mod</span></code>). The two-argument form <code class="docutils literal notranslate"><span class="pre">pow(base,</span> <span class="pre">exp)</span></code> is
  1562. equivalent to using the power operator: <code class="docutils literal notranslate"><span class="pre">base**exp</span></code>.</p>
  1563. <p>The arguments must have numeric types. With mixed operand types, the
  1564. coercion rules for binary arithmetic operators apply. For <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>
  1565. operands, the result has the same type as the operands (after coercion)
  1566. unless the second argument is negative; in that case, all arguments are
  1567. converted to float and a float result is delivered. For example, <code class="docutils literal notranslate"><span class="pre">pow(10,</span> <span class="pre">2)</span></code>
  1568. returns <code class="docutils literal notranslate"><span class="pre">100</span></code>, but <code class="docutils literal notranslate"><span class="pre">pow(10,</span> <span class="pre">-2)</span></code> returns <code class="docutils literal notranslate"><span class="pre">0.01</span></code>. For a negative base of
  1569. type <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> or <a class="reference internal" href="#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> and a non-integral exponent, a complex
  1570. result is delivered. For example, <code class="docutils literal notranslate"><span class="pre">pow(-9,</span> <span class="pre">0.5)</span></code> returns a value close
  1571. to <code class="docutils literal notranslate"><span class="pre">3j</span></code>.</p>
  1572. <p>For <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> operands <em>base</em> and <em>exp</em>, if <em>mod</em> is present, <em>mod</em> must
  1573. also be of integer type and <em>mod</em> must be nonzero. If <em>mod</em> is present and
  1574. <em>exp</em> is negative, <em>base</em> must be relatively prime to <em>mod</em>. In that case,
  1575. <code class="docutils literal notranslate"><span class="pre">pow(inv_base,</span> <span class="pre">-exp,</span> <span class="pre">mod)</span></code> is returned, where <em>inv_base</em> is an inverse to
  1576. <em>base</em> modulo <em>mod</em>.</p>
  1577. <p>Here’s an example of computing an inverse for <code class="docutils literal notranslate"><span class="pre">38</span></code> modulo <code class="docutils literal notranslate"><span class="pre">97</span></code>:</p>
  1578. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">pow</span><span class="p">(</span><span class="mi">38</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">mod</span><span class="o">=</span><span class="mi">97</span><span class="p">)</span>
  1579. <span class="go">23</span>
  1580. <span class="gp">&gt;&gt;&gt; </span><span class="mi">23</span> <span class="o">*</span> <span class="mi">38</span> <span class="o">%</span> <span class="mi">97</span> <span class="o">==</span> <span class="mi">1</span>
  1581. <span class="go">True</span>
  1582. </pre></div>
  1583. </div>
  1584. <div class="versionchanged">
  1585. <p><span class="versionmodified changed">Changed in version 3.8: </span>For <a class="reference internal" href="#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> operands, the three-argument form of <code class="docutils literal notranslate"><span class="pre">pow</span></code> now allows
  1586. the second argument to be negative, permitting computation of modular
  1587. inverses.</p>
  1588. </div>
  1589. <div class="versionchanged">
  1590. <p><span class="versionmodified changed">Changed in version 3.8: </span>Allow keyword arguments. Formerly, only positional arguments were
  1591. supported.</p>
  1592. </div>
  1593. </dd></dl>
  1594. <dl class="py function">
  1595. <dt class="sig sig-object py" id="print">
  1596. <span class="sig-name descname"><span class="pre">print</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">objects</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sep</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'</span> <span class="pre">'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">end</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'\n'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</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">flush</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="#print" title="Link to this definition">¶</a></dt>
  1597. <dd><p>Print <em>objects</em> to the text stream <em>file</em>, separated by <em>sep</em> and followed
  1598. by <em>end</em>. <em>sep</em>, <em>end</em>, <em>file</em>, and <em>flush</em>, if present, must be given as keyword
  1599. arguments.</p>
  1600. <p>All non-keyword arguments are converted to strings like <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> does and
  1601. written to the stream, separated by <em>sep</em> and followed by <em>end</em>. Both <em>sep</em>
  1602. and <em>end</em> must be strings; they can also be <code class="docutils literal notranslate"><span class="pre">None</span></code>, which means to use the
  1603. default values. If no <em>objects</em> are given, <a class="reference internal" href="#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> will just write
  1604. <em>end</em>.</p>
  1605. <p>The <em>file</em> argument must be an object with a <code class="docutils literal notranslate"><span class="pre">write(string)</span></code> method; if it
  1606. is not present or <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> will be used. Since printed
  1607. arguments are converted to text strings, <a class="reference internal" href="#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> cannot be used with
  1608. binary mode file objects. For these, use <code class="docutils literal notranslate"><span class="pre">file.write(...)</span></code> instead.</p>
  1609. <p>Output buffering is usually determined by <em>file</em>.
  1610. However, if <em>flush</em> is true, the stream is forcibly flushed.</p>
  1611. <div class="versionchanged">
  1612. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>flush</em> keyword argument.</p>
  1613. </div>
  1614. </dd></dl>
  1615. <dl class="py class">
  1616. <dt class="sig sig-object py" id="property">
  1617. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">property</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fget</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">fset</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">fdel</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">doc</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#property" title="Link to this definition">¶</a></dt>
  1618. <dd><p>Return a property attribute.</p>
  1619. <p><em>fget</em> is a function for getting an attribute value. <em>fset</em> is a function
  1620. for setting an attribute value. <em>fdel</em> is a function for deleting an attribute
  1621. value. And <em>doc</em> creates a docstring for the attribute.</p>
  1622. <p>A typical use is to define a managed attribute <code class="docutils literal notranslate"><span class="pre">x</span></code>:</p>
  1623. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
  1624. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1625. <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="kc">None</span>
  1626. <span class="k">def</span> <span class="nf">getx</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1627. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span>
  1628. <span class="k">def</span> <span class="nf">setx</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  1629. <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="n">value</span>
  1630. <span class="k">def</span> <span class="nf">delx</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1631. <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span>
  1632. <span class="n">x</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">getx</span><span class="p">,</span> <span class="n">setx</span><span class="p">,</span> <span class="n">delx</span><span class="p">,</span> <span class="s2">&quot;I&#39;m the &#39;x&#39; property.&quot;</span><span class="p">)</span>
  1633. </pre></div>
  1634. </div>
  1635. <p>If <em>c</em> is an instance of <em>C</em>, <code class="docutils literal notranslate"><span class="pre">c.x</span></code> will invoke the getter,
  1636. <code class="docutils literal notranslate"><span class="pre">c.x</span> <span class="pre">=</span> <span class="pre">value</span></code> will invoke the setter, and <code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">c.x</span></code> the deleter.</p>
  1637. <p>If given, <em>doc</em> will be the docstring of the property attribute. Otherwise, the
  1638. property will copy <em>fget</em>’s docstring (if it exists). This makes it possible to
  1639. create read-only properties easily using <a class="reference internal" href="#property" title="property"><code class="xref py py-func docutils literal notranslate"><span class="pre">property()</span></code></a> as a <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a>:</p>
  1640. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Parrot</span><span class="p">:</span>
  1641. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1642. <span class="bp">self</span><span class="o">.</span><span class="n">_voltage</span> <span class="o">=</span> <span class="mi">100000</span>
  1643. <span class="nd">@property</span>
  1644. <span class="k">def</span> <span class="nf">voltage</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1645. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Get the current voltage.&quot;&quot;&quot;</span>
  1646. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_voltage</span>
  1647. </pre></div>
  1648. </div>
  1649. <p>The <code class="docutils literal notranslate"><span class="pre">&#64;property</span></code> decorator turns the <code class="xref py py-meth docutils literal notranslate"><span class="pre">voltage()</span></code> method into a “getter”
  1650. for a read-only attribute with the same name, and it sets the docstring for
  1651. <em>voltage</em> to “Get the current voltage.”</p>
  1652. <dl class="py function">
  1653. <dt class="sig sig-object py" id="property.getter">
  1654. <span class="sig-prename descclassname"><span class="pre">&#64;</span></span><span class="sig-name descname"><span class="pre">getter</span></span><a class="headerlink" href="#property.getter" title="Link to this definition">¶</a></dt>
  1655. <dd></dd></dl>
  1656. <dl class="py function">
  1657. <dt class="sig sig-object py" id="property.setter">
  1658. <span class="sig-prename descclassname"><span class="pre">&#64;</span></span><span class="sig-name descname"><span class="pre">setter</span></span><a class="headerlink" href="#property.setter" title="Link to this definition">¶</a></dt>
  1659. <dd></dd></dl>
  1660. <dl class="py function">
  1661. <dt class="sig sig-object py" id="property.deleter">
  1662. <span class="sig-prename descclassname"><span class="pre">&#64;</span></span><span class="sig-name descname"><span class="pre">deleter</span></span><a class="headerlink" href="#property.deleter" title="Link to this definition">¶</a></dt>
  1663. <dd><p>A property object has <code class="docutils literal notranslate"><span class="pre">getter</span></code>, <code class="docutils literal notranslate"><span class="pre">setter</span></code>,
  1664. and <code class="docutils literal notranslate"><span class="pre">deleter</span></code> methods usable as decorators that create a
  1665. copy of the property with the corresponding accessor function set to the
  1666. decorated function. This is best explained with an example:</p>
  1667. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
  1668. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1669. <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="kc">None</span>
  1670. <span class="nd">@property</span>
  1671. <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1672. <span class="w"> </span><span class="sd">&quot;&quot;&quot;I&#39;m the &#39;x&#39; property.&quot;&quot;&quot;</span>
  1673. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span>
  1674. <span class="nd">@x</span><span class="o">.</span><span class="n">setter</span>
  1675. <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  1676. <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="n">value</span>
  1677. <span class="nd">@x</span><span class="o">.</span><span class="n">deleter</span>
  1678. <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1679. <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span>
  1680. </pre></div>
  1681. </div>
  1682. <p>This code is exactly equivalent to the first example. Be sure to give the
  1683. additional functions the same name as the original property (<code class="docutils literal notranslate"><span class="pre">x</span></code> in this
  1684. case.)</p>
  1685. <p>The returned property object also has the attributes <code class="docutils literal notranslate"><span class="pre">fget</span></code>, <code class="docutils literal notranslate"><span class="pre">fset</span></code>, and
  1686. <code class="docutils literal notranslate"><span class="pre">fdel</span></code> corresponding to the constructor arguments.</p>
  1687. </dd></dl>
  1688. <div class="versionchanged">
  1689. <p><span class="versionmodified changed">Changed in version 3.5: </span>The docstrings of property objects are now writeable.</p>
  1690. </div>
  1691. </dd></dl>
  1692. <dl class="py class" id="func-range">
  1693. <dt class="sig sig-object py">
  1694. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">range</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stop</span></span></em><span class="sig-paren">)</span></dt>
  1695. <dt class="sig sig-object py">
  1696. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">range</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">start</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stop</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">step</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span></dt>
  1697. <dd><p>Rather than being a function, <a class="reference internal" href="stdtypes.html#range" title="range"><code class="xref py py-class docutils literal notranslate"><span class="pre">range</span></code></a> is actually an immutable
  1698. sequence type, as documented in <a class="reference internal" href="stdtypes.html#typesseq-range"><span class="std std-ref">Ranges</span></a> and <a class="reference internal" href="stdtypes.html#typesseq"><span class="std std-ref">Sequence Types — list, tuple, range</span></a>.</p>
  1699. </dd></dl>
  1700. <dl class="py function">
  1701. <dt class="sig sig-object py" id="repr">
  1702. <span class="sig-name descname"><span class="pre">repr</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="#repr" title="Link to this definition">¶</a></dt>
  1703. <dd><p>Return a string containing a printable representation of an object. For many
  1704. types, this function makes an attempt to return a string that would yield an
  1705. object with the same value when passed to <a class="reference internal" href="#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>; otherwise, the
  1706. representation is a string enclosed in angle brackets that contains the name
  1707. of the type of the object together with additional information often
  1708. including the name and address of the object. A class can control what this
  1709. function returns for its instances
  1710. by defining a <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code></a> method.
  1711. If <a class="reference internal" href="sys.html#sys.displayhook" title="sys.displayhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.displayhook()</span></code></a> is not accessible, this function will raise
  1712. <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>.</p>
  1713. <p>This class has a custom representation that can be evaluated:</p>
  1714. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</span><span class="p">:</span>
  1715. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">age</span><span class="p">):</span>
  1716. <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
  1717. <span class="bp">self</span><span class="o">.</span><span class="n">age</span> <span class="o">=</span> <span class="n">age</span>
  1718. <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1719. <span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;Person(&#39;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s2">&#39;, </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">age</span><span class="si">}</span><span class="s2">)&quot;</span>
  1720. </pre></div>
  1721. </div>
  1722. </dd></dl>
  1723. <dl class="py function">
  1724. <dt class="sig sig-object py" id="reversed">
  1725. <span class="sig-name descname"><span class="pre">reversed</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">seq</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#reversed" title="Link to this definition">¶</a></dt>
  1726. <dd><p>Return a reverse <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a>. <em>seq</em> must be an object which has
  1727. a <a class="reference internal" href="../reference/datamodel.html#object.__reversed__" title="object.__reversed__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__reversed__()</span></code></a> method or supports the sequence protocol (the
  1728. <a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__len__()</span></code></a> method and the <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> method
  1729. with integer arguments starting at <code class="docutils literal notranslate"><span class="pre">0</span></code>).</p>
  1730. </dd></dl>
  1731. <dl class="py function">
  1732. <dt class="sig sig-object py" id="round">
  1733. <span class="sig-name descname"><span class="pre">round</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">number</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ndigits</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#round" title="Link to this definition">¶</a></dt>
  1734. <dd><p>Return <em>number</em> rounded to <em>ndigits</em> precision after the decimal
  1735. point. If <em>ndigits</em> is omitted or is <code class="docutils literal notranslate"><span class="pre">None</span></code>, it returns the
  1736. nearest integer to its input.</p>
  1737. <p>For the built-in types supporting <a class="reference internal" href="#round" title="round"><code class="xref py py-func docutils literal notranslate"><span class="pre">round()</span></code></a>, values are rounded to the
  1738. closest multiple of 10 to the power minus <em>ndigits</em>; if two multiples are
  1739. equally close, rounding is done toward the even choice (so, for example,
  1740. both <code class="docutils literal notranslate"><span class="pre">round(0.5)</span></code> and <code class="docutils literal notranslate"><span class="pre">round(-0.5)</span></code> are <code class="docutils literal notranslate"><span class="pre">0</span></code>, and <code class="docutils literal notranslate"><span class="pre">round(1.5)</span></code> is
  1741. <code class="docutils literal notranslate"><span class="pre">2</span></code>). Any integer value is valid for <em>ndigits</em> (positive, zero, or
  1742. negative). The return value is an integer if <em>ndigits</em> is omitted or
  1743. <code class="docutils literal notranslate"><span class="pre">None</span></code>.
  1744. Otherwise, the return value has the same type as <em>number</em>.</p>
  1745. <p>For a general Python object <code class="docutils literal notranslate"><span class="pre">number</span></code>, <code class="docutils literal notranslate"><span class="pre">round</span></code> delegates to
  1746. <code class="docutils literal notranslate"><span class="pre">number.__round__</span></code>.</p>
  1747. <div class="admonition note">
  1748. <p class="admonition-title">Note</p>
  1749. <p>The behavior of <a class="reference internal" href="#round" title="round"><code class="xref py py-func docutils literal notranslate"><span class="pre">round()</span></code></a> for floats can be surprising: for example,
  1750. <code class="docutils literal notranslate"><span class="pre">round(2.675,</span> <span class="pre">2)</span></code> gives <code class="docutils literal notranslate"><span class="pre">2.67</span></code> instead of the expected <code class="docutils literal notranslate"><span class="pre">2.68</span></code>.
  1751. This is not a bug: it’s a result of the fact that most decimal fractions
  1752. can’t be represented exactly as a float. See <a class="reference internal" href="../tutorial/floatingpoint.html#tut-fp-issues"><span class="std std-ref">Floating Point Arithmetic: Issues and Limitations</span></a> for
  1753. more information.</p>
  1754. </div>
  1755. </dd></dl>
  1756. <dl class="py class" id="func-set">
  1757. <dt class="sig sig-object py">
  1758. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">set</span></span></dt>
  1759. <dt class="sig sig-object py">
  1760. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">set</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="sig-paren">)</span></dt>
  1761. <dd><p>Return a new <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> object, optionally with elements taken from
  1762. <em>iterable</em>. <code class="docutils literal notranslate"><span class="pre">set</span></code> is a built-in class. See <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> and
  1763. <a class="reference internal" href="stdtypes.html#types-set"><span class="std std-ref">Set Types — set, frozenset</span></a> for documentation about this class.</p>
  1764. <p>For other containers see the built-in <a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal notranslate"><span class="pre">frozenset</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>,
  1765. <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>, and <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> classes, as well as the <a class="reference internal" href="collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a>
  1766. module.</p>
  1767. </dd></dl>
  1768. <dl class="py function">
  1769. <dt class="sig sig-object py" id="setattr">
  1770. <span class="sig-name descname"><span class="pre">setattr</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">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#setattr" title="Link to this definition">¶</a></dt>
  1771. <dd><p>This is the counterpart of <a class="reference internal" href="#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a>. The arguments are an object, a
  1772. string, and an arbitrary value. The string may name an existing attribute or a
  1773. new attribute. The function assigns the value to the attribute, provided the
  1774. object allows it. For example, <code class="docutils literal notranslate"><span class="pre">setattr(x,</span> <span class="pre">'foobar',</span> <span class="pre">123)</span></code> is equivalent to
  1775. <code class="docutils literal notranslate"><span class="pre">x.foobar</span> <span class="pre">=</span> <span class="pre">123</span></code>.</p>
  1776. <p><em>name</em> need not be a Python identifier as defined in <a class="reference internal" href="../reference/lexical_analysis.html#identifiers"><span class="std std-ref">Identifiers and keywords</span></a>
  1777. unless the object chooses to enforce that, for example in a custom
  1778. <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code></a> or via <a class="reference internal" href="../reference/datamodel.html#object.__slots__" title="object.__slots__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__slots__</span></code></a>.
  1779. An attribute whose name is not an identifier will not be accessible using
  1780. the dot notation, but is accessible through <a class="reference internal" href="#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> etc..</p>
  1781. <div class="admonition note">
  1782. <p class="admonition-title">Note</p>
  1783. <p>Since <a class="reference internal" href="../reference/expressions.html#private-name-mangling"><span class="std std-ref">private name mangling</span></a> happens at
  1784. compilation time, one must manually mangle a private attribute’s
  1785. (attributes with two leading underscores) name in order to set it with
  1786. <a class="reference internal" href="#setattr" title="setattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">setattr()</span></code></a>.</p>
  1787. </div>
  1788. </dd></dl>
  1789. <dl class="py class">
  1790. <dt class="sig sig-object py" id="slice">
  1791. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">slice</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stop</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#slice" title="Link to this definition">¶</a></dt>
  1792. <dt class="sig sig-object py">
  1793. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">slice</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">start</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stop</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">step</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span></dt>
  1794. <dd><p>Return a <a class="reference internal" href="../glossary.html#term-slice"><span class="xref std std-term">slice</span></a> object representing the set of indices specified by
  1795. <code class="docutils literal notranslate"><span class="pre">range(start,</span> <span class="pre">stop,</span> <span class="pre">step)</span></code>. The <em>start</em> and <em>step</em> arguments default to
  1796. <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1797. <dl class="py attribute">
  1798. <dt class="sig sig-object py" id="slice.start">
  1799. <span class="sig-name descname"><span class="pre">start</span></span><a class="headerlink" href="#slice.start" title="Link to this definition">¶</a></dt>
  1800. <dd></dd></dl>
  1801. <dl class="py attribute">
  1802. <dt class="sig sig-object py" id="slice.stop">
  1803. <span class="sig-name descname"><span class="pre">stop</span></span><a class="headerlink" href="#slice.stop" title="Link to this definition">¶</a></dt>
  1804. <dd></dd></dl>
  1805. <dl class="py attribute">
  1806. <dt class="sig sig-object py" id="slice.step">
  1807. <span class="sig-name descname"><span class="pre">step</span></span><a class="headerlink" href="#slice.step" title="Link to this definition">¶</a></dt>
  1808. <dd><p>Slice objects have read-only data attributes <code class="xref py py-attr docutils literal notranslate"><span class="pre">start</span></code>,
  1809. <code class="xref py py-attr docutils literal notranslate"><span class="pre">stop</span></code>, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">step</span></code> which merely return the argument
  1810. values (or their default). They have no other explicit functionality;
  1811. however, they are used by NumPy and other third-party packages.</p>
  1812. </dd></dl>
  1813. <p>Slice objects are also generated when extended indexing syntax is used. For
  1814. example: <code class="docutils literal notranslate"><span class="pre">a[start:stop:step]</span></code> or <code class="docutils literal notranslate"><span class="pre">a[start:stop,</span> <span class="pre">i]</span></code>. See
  1815. <a class="reference internal" href="itertools.html#itertools.islice" title="itertools.islice"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.islice()</span></code></a> for an alternate version that returns an
  1816. <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a>.</p>
  1817. <div class="versionchanged">
  1818. <p><span class="versionmodified changed">Changed in version 3.12: </span>Slice objects are now <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a> (provided <a class="reference internal" href="#slice.start" title="slice.start"><code class="xref py py-attr docutils literal notranslate"><span class="pre">start</span></code></a>,
  1819. <a class="reference internal" href="#slice.stop" title="slice.stop"><code class="xref py py-attr docutils literal notranslate"><span class="pre">stop</span></code></a>, and <a class="reference internal" href="#slice.step" title="slice.step"><code class="xref py py-attr docutils literal notranslate"><span class="pre">step</span></code></a> are hashable).</p>
  1820. </div>
  1821. </dd></dl>
  1822. <dl class="py function">
  1823. <dt class="sig sig-object py" id="sorted">
  1824. <span class="sig-name descname"><span class="pre">sorted</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</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">key</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">reverse</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="#sorted" title="Link to this definition">¶</a></dt>
  1825. <dd><p>Return a new sorted list from the items in <em>iterable</em>.</p>
  1826. <p>Has two optional arguments which must be specified as keyword arguments.</p>
  1827. <p><em>key</em> specifies a function of one argument that is used to extract a comparison
  1828. key from each element in <em>iterable</em> (for example, <code class="docutils literal notranslate"><span class="pre">key=str.lower</span></code>). The
  1829. default value is <code class="docutils literal notranslate"><span class="pre">None</span></code> (compare the elements directly).</p>
  1830. <p><em>reverse</em> is a boolean value. If set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, then the list elements are
  1831. sorted as if each comparison were reversed.</p>
  1832. <p>Use <a class="reference internal" href="functools.html#functools.cmp_to_key" title="functools.cmp_to_key"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.cmp_to_key()</span></code></a> to convert an old-style <em>cmp</em> function to a
  1833. <em>key</em> function.</p>
  1834. <p>The built-in <a class="reference internal" href="#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a> function is guaranteed to be stable. A sort is
  1835. stable if it guarantees not to change the relative order of elements that
  1836. compare equal — this is helpful for sorting in multiple passes (for
  1837. example, sort by department, then by salary grade).</p>
  1838. <p>The sort algorithm uses only <code class="docutils literal notranslate"><span class="pre">&lt;</span></code> comparisons between items. While
  1839. defining an <a class="reference internal" href="../reference/datamodel.html#object.__lt__" title="object.__lt__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__lt__()</span></code></a> method will suffice for sorting,
  1840. <span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-0008/"><strong>PEP 8</strong></a> recommends that all six <a class="reference internal" href="../reference/expressions.html#comparisons"><span class="std std-ref">rich comparisons</span></a> be implemented. This will help avoid bugs when using
  1841. the same data with other ordering tools such as <a class="reference internal" href="#max" title="max"><code class="xref py py-func docutils literal notranslate"><span class="pre">max()</span></code></a> that rely
  1842. on a different underlying method. Implementing all six comparisons
  1843. also helps avoid confusion for mixed type comparisons which can call
  1844. reflected the <a class="reference internal" href="../reference/datamodel.html#object.__gt__" title="object.__gt__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__gt__()</span></code></a> method.</p>
  1845. <p>For sorting examples and a brief sorting tutorial, see <a class="reference internal" href="../howto/sorting.html#sortinghowto"><span class="std std-ref">Sorting Techniques</span></a>.</p>
  1846. </dd></dl>
  1847. <dl class="py function">
  1848. <dt class="sig sig-object py" id="staticmethod">
  1849. <span class="sig-prename descclassname"><span class="pre">&#64;</span></span><span class="sig-name descname"><span class="pre">staticmethod</span></span><a class="headerlink" href="#staticmethod" title="Link to this definition">¶</a></dt>
  1850. <dd><p>Transform a method into a static method.</p>
  1851. <p>A static method does not receive an implicit first argument. To declare a static
  1852. method, use this idiom:</p>
  1853. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
  1854. <span class="nd">@staticmethod</span>
  1855. <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">argN</span><span class="p">):</span> <span class="o">...</span>
  1856. </pre></div>
  1857. </div>
  1858. <p>The <code class="docutils literal notranslate"><span class="pre">&#64;staticmethod</span></code> form is a function <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a> – see
  1859. <a class="reference internal" href="../reference/compound_stmts.html#function"><span class="std std-ref">Function definitions</span></a> for details.</p>
  1860. <p>A static method can be called either on the class (such as <code class="docutils literal notranslate"><span class="pre">C.f()</span></code>) or on
  1861. an instance (such as <code class="docutils literal notranslate"><span class="pre">C().f()</span></code>). Moreover, they can be called as regular
  1862. functions (such as <code class="docutils literal notranslate"><span class="pre">f()</span></code>).</p>
  1863. <p>Static methods in Python are similar to those found in Java or C++. Also, see
  1864. <a class="reference internal" href="#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a> for a variant that is useful for creating alternate class
  1865. constructors.</p>
  1866. <p>Like all decorators, it is also possible to call <code class="docutils literal notranslate"><span class="pre">staticmethod</span></code> as
  1867. a regular function and do something with its result. This is needed
  1868. in some cases where you need a reference to a function from a class
  1869. body and you want to avoid the automatic transformation to instance
  1870. method. For these cases, use this idiom:</p>
  1871. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">regular_function</span><span class="p">():</span>
  1872. <span class="o">...</span>
  1873. <span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
  1874. <span class="n">method</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">regular_function</span><span class="p">)</span>
  1875. </pre></div>
  1876. </div>
  1877. <p>For more information on static methods, see <a class="reference internal" href="../reference/datamodel.html#types"><span class="std std-ref">The standard type hierarchy</span></a>.</p>
  1878. <div class="versionchanged">
  1879. <p><span class="versionmodified changed">Changed in version 3.10: </span>Static methods now inherit the method attributes (<code class="docutils literal notranslate"><span class="pre">__module__</span></code>,
  1880. <code class="docutils literal notranslate"><span class="pre">__name__</span></code>, <code class="docutils literal notranslate"><span class="pre">__qualname__</span></code>, <code class="docutils literal notranslate"><span class="pre">__doc__</span></code> and <code class="docutils literal notranslate"><span class="pre">__annotations__</span></code>),
  1881. have a new <code class="docutils literal notranslate"><span class="pre">__wrapped__</span></code> attribute, and are now callable as regular
  1882. functions.</p>
  1883. </div>
  1884. </dd></dl>
  1885. <dl class="py class" id="func-str">
  1886. <span id="index-11"></span><dt class="sig sig-object py">
  1887. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">str</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em><span class="sig-paren">)</span></dt>
  1888. <dt class="sig sig-object py">
  1889. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">str</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'utf-8'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span></dt>
  1890. <dd><p>Return a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> version of <em>object</em>. See <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> for details.</p>
  1891. <p><code class="docutils literal notranslate"><span class="pre">str</span></code> is the built-in string <a class="reference internal" href="../glossary.html#term-class"><span class="xref std std-term">class</span></a>. For general information
  1892. about strings, see <a class="reference internal" href="stdtypes.html#textseq"><span class="std std-ref">Text Sequence Type — str</span></a>.</p>
  1893. </dd></dl>
  1894. <dl class="py function">
  1895. <dt class="sig sig-object py" id="sum">
  1896. <span class="sig-name descname"><span class="pre">sum</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</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">start</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sum" title="Link to this definition">¶</a></dt>
  1897. <dd><p>Sums <em>start</em> and the items of an <em>iterable</em> from left to right and returns the
  1898. total. The <em>iterable</em>’s items are normally numbers, and the start value is not
  1899. allowed to be a string.</p>
  1900. <p>For some use cases, there are good alternatives to <a class="reference internal" href="#sum" title="sum"><code class="xref py py-func docutils literal notranslate"><span class="pre">sum()</span></code></a>.
  1901. The preferred, fast way to concatenate a sequence of strings is by calling
  1902. <code class="docutils literal notranslate"><span class="pre">''.join(sequence)</span></code>. To add floating point values with extended precision,
  1903. see <a class="reference internal" href="math.html#math.fsum" title="math.fsum"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.fsum()</span></code></a>. To concatenate a series of iterables, consider using
  1904. <a class="reference internal" href="itertools.html#itertools.chain" title="itertools.chain"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.chain()</span></code></a>.</p>
  1905. <div class="versionchanged">
  1906. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <em>start</em> parameter can be specified as a keyword argument.</p>
  1907. </div>
  1908. <div class="versionchanged">
  1909. <p><span class="versionmodified changed">Changed in version 3.12: </span>Summation of floats switched to an algorithm
  1910. that gives higher accuracy on most builds.</p>
  1911. </div>
  1912. </dd></dl>
  1913. <dl class="py class">
  1914. <dt class="sig sig-object py" id="super">
  1915. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">super</span></span><a class="headerlink" href="#super" title="Link to this definition">¶</a></dt>
  1916. <dt class="sig sig-object py">
  1917. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">super</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">type</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">object_or_type</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span></dt>
  1918. <dd><p>Return a proxy object that delegates method calls to a parent or sibling
  1919. class of <em>type</em>. This is useful for accessing inherited methods that have
  1920. been overridden in a class.</p>
  1921. <p>The <em>object_or_type</em> determines the <a class="reference internal" href="../glossary.html#term-method-resolution-order"><span class="xref std std-term">method resolution order</span></a>
  1922. to be searched. The search starts from the class right after the
  1923. <em>type</em>.</p>
  1924. <p>For example, if <a class="reference internal" href="stdtypes.html#class.__mro__" title="class.__mro__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__mro__</span></code></a> of <em>object_or_type</em> is
  1925. <code class="docutils literal notranslate"><span class="pre">D</span> <span class="pre">-&gt;</span> <span class="pre">B</span> <span class="pre">-&gt;</span> <span class="pre">C</span> <span class="pre">-&gt;</span> <span class="pre">A</span> <span class="pre">-&gt;</span> <span class="pre">object</span></code> and the value of <em>type</em> is <code class="docutils literal notranslate"><span class="pre">B</span></code>,
  1926. then <a class="reference internal" href="#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a> searches <code class="docutils literal notranslate"><span class="pre">C</span> <span class="pre">-&gt;</span> <span class="pre">A</span> <span class="pre">-&gt;</span> <span class="pre">object</span></code>.</p>
  1927. <p>The <a class="reference internal" href="stdtypes.html#class.__mro__" title="class.__mro__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__mro__</span></code></a> attribute of the <em>object_or_type</em> lists the method
  1928. resolution search order used by both <a class="reference internal" href="#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> and <a class="reference internal" href="#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a>. The
  1929. attribute is dynamic and can change whenever the inheritance hierarchy is
  1930. updated.</p>
  1931. <p>If the second argument is omitted, the super object returned is unbound. If
  1932. the second argument is an object, <code class="docutils literal notranslate"><span class="pre">isinstance(obj,</span> <span class="pre">type)</span></code> must be true. If
  1933. the second argument is a type, <code class="docutils literal notranslate"><span class="pre">issubclass(type2,</span> <span class="pre">type)</span></code> must be true (this
  1934. is useful for classmethods).</p>
  1935. <p>There are two typical use cases for <em>super</em>. In a class hierarchy with
  1936. single inheritance, <em>super</em> can be used to refer to parent classes without
  1937. naming them explicitly, thus making the code more maintainable. This use
  1938. closely parallels the use of <em>super</em> in other programming languages.</p>
  1939. <p>The second use case is to support cooperative multiple inheritance in a
  1940. dynamic execution environment. This use case is unique to Python and is
  1941. not found in statically compiled languages or languages that only support
  1942. single inheritance. This makes it possible to implement “diamond diagrams”
  1943. where multiple base classes implement the same method. Good design dictates
  1944. that such implementations have the same calling signature in every case (because the
  1945. order of calls is determined at runtime, because that order adapts
  1946. to changes in the class hierarchy, and because that order can include
  1947. sibling classes that are unknown prior to runtime).</p>
  1948. <p>For both use cases, a typical superclass call looks like this:</p>
  1949. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="n">B</span><span class="p">):</span>
  1950. <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  1951. <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">method</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="c1"># This does the same thing as:</span>
  1952. <span class="c1"># super(C, self).method(arg)</span>
  1953. </pre></div>
  1954. </div>
  1955. <p>In addition to method lookups, <a class="reference internal" href="#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a> also works for attribute
  1956. lookups. One possible use case for this is calling <a class="reference internal" href="../glossary.html#term-descriptor"><span class="xref std std-term">descriptors</span></a>
  1957. in a parent or sibling class.</p>
  1958. <p>Note that <a class="reference internal" href="#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a> is implemented as part of the binding process for
  1959. explicit dotted attribute lookups such as <code class="docutils literal notranslate"><span class="pre">super().__getitem__(name)</span></code>.
  1960. It does so by implementing its own <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code></a> method
  1961. for searching
  1962. classes in a predictable order that supports cooperative multiple inheritance.
  1963. Accordingly, <a class="reference internal" href="#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a> is undefined for implicit lookups using statements or
  1964. operators such as <code class="docutils literal notranslate"><span class="pre">super()[name]</span></code>.</p>
  1965. <p>Also note that, aside from the zero argument form, <a class="reference internal" href="#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a> is not
  1966. limited to use inside methods. The two argument form specifies the
  1967. arguments exactly and makes the appropriate references. The zero
  1968. argument form only works inside a class definition, as the compiler fills
  1969. in the necessary details to correctly retrieve the class being defined,
  1970. as well as accessing the current instance for ordinary methods.</p>
  1971. <p>For practical suggestions on how to design cooperative classes using
  1972. <a class="reference internal" href="#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a>, see <a class="reference external" href="https://rhettinger.wordpress.com/2011/05/26/super-considered-super/">guide to using super()</a>.</p>
  1973. </dd></dl>
  1974. <dl class="py class" id="func-tuple">
  1975. <dt class="sig sig-object py">
  1976. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">tuple</span></span></dt>
  1977. <dt class="sig sig-object py">
  1978. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">tuple</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="sig-paren">)</span></dt>
  1979. <dd><p>Rather than being a function, <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> is actually an immutable
  1980. sequence type, as documented in <a class="reference internal" href="stdtypes.html#typesseq-tuple"><span class="std std-ref">Tuples</span></a> and <a class="reference internal" href="stdtypes.html#typesseq"><span class="std std-ref">Sequence Types — list, tuple, range</span></a>.</p>
  1981. </dd></dl>
  1982. <dl class="py class">
  1983. <dt class="sig sig-object py" id="type">
  1984. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">type</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="#type" title="Link to this definition">¶</a></dt>
  1985. <dt class="sig sig-object py">
  1986. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">type</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">bases</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dict</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span></dt>
  1987. <dd><p id="index-12">With one argument, return the type of an <em>object</em>. The return value is a
  1988. type object and generally the same object as returned by
  1989. <a class="reference internal" href="stdtypes.html#instance.__class__" title="instance.__class__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">object.__class__</span></code></a>.</p>
  1990. <p>The <a class="reference internal" href="#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a> built-in function is recommended for testing the type
  1991. of an object, because it takes subclasses into account.</p>
  1992. <p>With three arguments, return a new type object. This is essentially a
  1993. dynamic form of the <a class="reference internal" href="../reference/compound_stmts.html#class"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">class</span></code></a> statement. The <em>name</em> string is
  1994. the class name and becomes the <a class="reference internal" href="stdtypes.html#definition.__name__" title="definition.__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a> attribute.
  1995. The <em>bases</em> tuple contains the base classes and becomes the
  1996. <a class="reference internal" href="stdtypes.html#class.__bases__" title="class.__bases__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__bases__</span></code></a> attribute; if empty, <a class="reference internal" href="#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a>, the
  1997. ultimate base of all classes, is added. The <em>dict</em> dictionary contains
  1998. attribute and method definitions for the class body; it may be copied
  1999. or wrapped before becoming the <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> attribute.
  2000. The following two statements create identical <a class="reference internal" href="#type" title="type"><code class="xref py py-class docutils literal notranslate"><span class="pre">type</span></code></a> objects:</p>
  2001. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">X</span><span class="p">:</span>
  2002. <span class="gp">... </span> <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
  2003. <span class="gp">...</span>
  2004. <span class="gp">&gt;&gt;&gt; </span><span class="n">X</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;X&#39;</span><span class="p">,</span> <span class="p">(),</span> <span class="nb">dict</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
  2005. </pre></div>
  2006. </div>
  2007. <p>See also <a class="reference internal" href="stdtypes.html#bltin-type-objects"><span class="std std-ref">Type Objects</span></a>.</p>
  2008. <p>Keyword arguments provided to the three argument form are passed to the
  2009. appropriate metaclass machinery (usually <a class="reference internal" href="../reference/datamodel.html#object.__init_subclass__" title="object.__init_subclass__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__init_subclass__()</span></code></a>)
  2010. in the same way that keywords in a class
  2011. definition (besides <em>metaclass</em>) would.</p>
  2012. <p>See also <a class="reference internal" href="../reference/datamodel.html#class-customization"><span class="std std-ref">Customizing class creation</span></a>.</p>
  2013. <div class="versionchanged">
  2014. <p><span class="versionmodified changed">Changed in version 3.6: </span>Subclasses of <a class="reference internal" href="#type" title="type"><code class="xref py py-class docutils literal notranslate"><span class="pre">type</span></code></a> which don’t override <code class="docutils literal notranslate"><span class="pre">type.__new__</span></code> may no
  2015. longer use the one-argument form to get the type of an object.</p>
  2016. </div>
  2017. </dd></dl>
  2018. <dl class="py function">
  2019. <dt class="sig sig-object py" id="vars">
  2020. <span class="sig-name descname"><span class="pre">vars</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#vars" title="Link to this definition">¶</a></dt>
  2021. <dt class="sig sig-object py">
  2022. <span class="sig-name descname"><span class="pre">vars</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></dt>
  2023. <dd><p>Return the <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> attribute for a module, class, instance,
  2024. or any other object with a <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> attribute.</p>
  2025. <p>Objects such as modules and instances have an updateable <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a>
  2026. attribute; however, other objects may have write restrictions on their
  2027. <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> attributes (for example, classes use a
  2028. <a class="reference internal" href="types.html#types.MappingProxyType" title="types.MappingProxyType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.MappingProxyType</span></code></a> to prevent direct dictionary updates).</p>
  2029. <p>Without an argument, <a class="reference internal" href="#vars" title="vars"><code class="xref py py-func docutils literal notranslate"><span class="pre">vars()</span></code></a> acts like <a class="reference internal" href="#locals" title="locals"><code class="xref py py-func docutils literal notranslate"><span class="pre">locals()</span></code></a>. Note, the
  2030. locals dictionary is only useful for reads since updates to the locals
  2031. dictionary are ignored.</p>
  2032. <p>A <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception is raised if an object is specified but
  2033. it doesn’t have a <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> attribute (for example, if
  2034. its class defines the <a class="reference internal" href="../reference/datamodel.html#object.__slots__" title="object.__slots__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__slots__</span></code></a> attribute).</p>
  2035. </dd></dl>
  2036. <dl class="py function">
  2037. <dt class="sig sig-object py" id="zip">
  2038. <span class="sig-name descname"><span class="pre">zip</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">iterables</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">strict</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="#zip" title="Link to this definition">¶</a></dt>
  2039. <dd><p>Iterate over several iterables in parallel, producing tuples with an item
  2040. from each one.</p>
  2041. <p>Example:</p>
  2042. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;sugar&#39;</span><span class="p">,</span> <span class="s1">&#39;spice&#39;</span><span class="p">,</span> <span class="s1">&#39;everything nice&#39;</span><span class="p">]):</span>
  2043. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
  2044. <span class="gp">...</span>
  2045. <span class="go">(1, &#39;sugar&#39;)</span>
  2046. <span class="go">(2, &#39;spice&#39;)</span>
  2047. <span class="go">(3, &#39;everything nice&#39;)</span>
  2048. </pre></div>
  2049. </div>
  2050. <p>More formally: <a class="reference internal" href="#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> returns an iterator of tuples, where the <em>i</em>-th
  2051. tuple contains the <em>i</em>-th element from each of the argument iterables.</p>
  2052. <p>Another way to think of <a class="reference internal" href="#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> is that it turns rows into columns, and
  2053. columns into rows. This is similar to <a class="reference external" href="https://en.wikipedia.org/wiki/Transpose">transposing a matrix</a>.</p>
  2054. <p><a class="reference internal" href="#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> is lazy: The elements won’t be processed until the iterable is
  2055. iterated on, e.g. by a <code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code> loop or by wrapping in a
  2056. <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>.</p>
  2057. <p>One thing to consider is that the iterables passed to <a class="reference internal" href="#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> could have
  2058. different lengths; sometimes by design, and sometimes because of a bug in
  2059. the code that prepared these iterables. Python offers three different
  2060. approaches to dealing with this issue:</p>
  2061. <ul>
  2062. <li><p>By default, <a class="reference internal" href="#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> stops when the shortest iterable is exhausted.
  2063. It will ignore the remaining items in the longer iterables, cutting off
  2064. the result to the length of the shortest iterable:</p>
  2065. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="p">[</span><span class="s1">&#39;fee&#39;</span><span class="p">,</span> <span class="s1">&#39;fi&#39;</span><span class="p">,</span> <span class="s1">&#39;fo&#39;</span><span class="p">,</span> <span class="s1">&#39;fum&#39;</span><span class="p">]))</span>
  2066. <span class="go">[(0, &#39;fee&#39;), (1, &#39;fi&#39;), (2, &#39;fo&#39;)]</span>
  2067. </pre></div>
  2068. </div>
  2069. </li>
  2070. <li><p><a class="reference internal" href="#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> is often used in cases where the iterables are assumed to be
  2071. of equal length. In such cases, it’s recommended to use the <code class="docutils literal notranslate"><span class="pre">strict=True</span></code>
  2072. option. Its output is the same as regular <a class="reference internal" href="#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a>:</p>
  2073. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">((</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">strict</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
  2074. <span class="go">[(&#39;a&#39;, 1), (&#39;b&#39;, 2), (&#39;c&#39;, 3)]</span>
  2075. </pre></div>
  2076. </div>
  2077. <p>Unlike the default behavior, it raises a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if one iterable
  2078. is exhausted before the others:</p>
  2079. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="p">[</span><span class="s1">&#39;fee&#39;</span><span class="p">,</span> <span class="s1">&#39;fi&#39;</span><span class="p">,</span> <span class="s1">&#39;fo&#39;</span><span class="p">,</span> <span class="s1">&#39;fum&#39;</span><span class="p">],</span> <span class="n">strict</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
  2080. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
  2081. <span class="gp">...</span>
  2082. <span class="go">(0, &#39;fee&#39;)</span>
  2083. <span class="go">(1, &#39;fi&#39;)</span>
  2084. <span class="go">(2, &#39;fo&#39;)</span>
  2085. <span class="gt">Traceback (most recent call last):</span>
  2086. <span class="w"> </span><span class="c">...</span>
  2087. <span class="gr">ValueError</span>: <span class="n">zip() argument 2 is longer than argument 1</span>
  2088. </pre></div>
  2089. </div>
  2090. <p>Without the <code class="docutils literal notranslate"><span class="pre">strict=True</span></code> argument, any bug that results in iterables of
  2091. different lengths will be silenced, possibly manifesting as a hard-to-find
  2092. bug in another part of the program.</p>
  2093. </li>
  2094. <li><p>Shorter iterables can be padded with a constant value to make all the
  2095. iterables have the same length. This is done by
  2096. <a class="reference internal" href="itertools.html#itertools.zip_longest" title="itertools.zip_longest"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.zip_longest()</span></code></a>.</p></li>
  2097. </ul>
  2098. <p>Edge cases: With a single iterable argument, <a class="reference internal" href="#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> returns an
  2099. iterator of 1-tuples. With no arguments, it returns an empty iterator.</p>
  2100. <p>Tips and tricks:</p>
  2101. <ul>
  2102. <li><p>The left-to-right evaluation order of the iterables is guaranteed. This
  2103. makes possible an idiom for clustering a data series into n-length groups
  2104. using <code class="docutils literal notranslate"><span class="pre">zip(*[iter(s)]*n,</span> <span class="pre">strict=True)</span></code>. This repeats the <em>same</em> iterator
  2105. <code class="docutils literal notranslate"><span class="pre">n</span></code> times so that each output tuple has the result of <code class="docutils literal notranslate"><span class="pre">n</span></code> calls to the
  2106. iterator. This has the effect of dividing the input into n-length chunks.</p></li>
  2107. <li><p><a class="reference internal" href="#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> in conjunction with the <code class="docutils literal notranslate"><span class="pre">*</span></code> operator can be used to unzip a
  2108. list:</p>
  2109. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
  2110. <span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]</span>
  2111. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">))</span>
  2112. <span class="go">[(1, 4), (2, 5), (3, 6)]</span>
  2113. <span class="gp">&gt;&gt;&gt; </span><span class="n">x2</span><span class="p">,</span> <span class="n">y2</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="nb">zip</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">))</span>
  2114. <span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span><span class="n">x2</span><span class="p">)</span> <span class="ow">and</span> <span class="n">y</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span><span class="n">y2</span><span class="p">)</span>
  2115. <span class="go">True</span>
  2116. </pre></div>
  2117. </div>
  2118. </li>
  2119. </ul>
  2120. <div class="versionchanged">
  2121. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <code class="docutils literal notranslate"><span class="pre">strict</span></code> argument.</p>
  2122. </div>
  2123. </dd></dl>
  2124. <dl class="py function">
  2125. <dt class="sig sig-object py" id="import__">
  2126. <span class="sig-name descname"><span class="pre">__import__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globals</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">locals</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">fromlist</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#import__" title="Link to this definition">¶</a></dt>
  2127. <dd><div class="admonition note" id="index-13">
  2128. <p class="admonition-title">Note</p>
  2129. <p>This is an advanced function that is not needed in everyday Python
  2130. programming, unlike <a class="reference internal" href="importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a>.</p>
  2131. </div>
  2132. <p>This function is invoked by the <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement. It can be
  2133. replaced (by importing the <a class="reference internal" href="builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> module and assigning to
  2134. <code class="docutils literal notranslate"><span class="pre">builtins.__import__</span></code>) in order to change semantics of the
  2135. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> statement, but doing so is <strong>strongly</strong> discouraged as it
  2136. is usually simpler to use import hooks (see <span class="target" id="index-14"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a>) to attain the same
  2137. goals and does not cause issues with code which assumes the default import
  2138. implementation is in use. Direct use of <a class="reference internal" href="#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> is also
  2139. discouraged in favor of <a class="reference internal" href="importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a>.</p>
  2140. <p>The function imports the module <em>name</em>, potentially using the given <em>globals</em>
  2141. and <em>locals</em> to determine how to interpret the name in a package context.
  2142. The <em>fromlist</em> gives the names of objects or submodules that should be
  2143. imported from the module given by <em>name</em>. The standard implementation does
  2144. not use its <em>locals</em> argument at all and uses its <em>globals</em> only to
  2145. determine the package context of the <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement.</p>
  2146. <p><em>level</em> specifies whether to use absolute or relative imports. <code class="docutils literal notranslate"><span class="pre">0</span></code> (the
  2147. default) means only perform absolute imports. Positive values for
  2148. <em>level</em> indicate the number of parent directories to search relative to the
  2149. directory of the module calling <a class="reference internal" href="#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> (see <span class="target" id="index-15"></span><a class="pep reference external" href="https://peps.python.org/pep-0328/"><strong>PEP 328</strong></a> for the
  2150. details).</p>
  2151. <p>When the <em>name</em> variable is of the form <code class="docutils literal notranslate"><span class="pre">package.module</span></code>, normally, the
  2152. top-level package (the name up till the first dot) is returned, <em>not</em> the
  2153. module named by <em>name</em>. However, when a non-empty <em>fromlist</em> argument is
  2154. given, the module named by <em>name</em> is returned.</p>
  2155. <p>For example, the statement <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">spam</span></code> results in bytecode resembling the
  2156. following code:</p>
  2157. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">spam</span> <span class="o">=</span> <span class="nb">__import__</span><span class="p">(</span><span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="nb">globals</span><span class="p">(),</span> <span class="nb">locals</span><span class="p">(),</span> <span class="p">[],</span> <span class="mi">0</span><span class="p">)</span>
  2158. </pre></div>
  2159. </div>
  2160. <p>The statement <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">spam.ham</span></code> results in this call:</p>
  2161. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">spam</span> <span class="o">=</span> <span class="nb">__import__</span><span class="p">(</span><span class="s1">&#39;spam.ham&#39;</span><span class="p">,</span> <span class="nb">globals</span><span class="p">(),</span> <span class="nb">locals</span><span class="p">(),</span> <span class="p">[],</span> <span class="mi">0</span><span class="p">)</span>
  2162. </pre></div>
  2163. </div>
  2164. <p>Note how <a class="reference internal" href="#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> returns the toplevel module here because this is
  2165. the object that is bound to a name by the <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement.</p>
  2166. <p>On the other hand, the statement <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">spam.ham</span> <span class="pre">import</span> <span class="pre">eggs,</span> <span class="pre">sausage</span> <span class="pre">as</span>
  2167. <span class="pre">saus</span></code> results in</p>
  2168. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">_temp</span> <span class="o">=</span> <span class="nb">__import__</span><span class="p">(</span><span class="s1">&#39;spam.ham&#39;</span><span class="p">,</span> <span class="nb">globals</span><span class="p">(),</span> <span class="nb">locals</span><span class="p">(),</span> <span class="p">[</span><span class="s1">&#39;eggs&#39;</span><span class="p">,</span> <span class="s1">&#39;sausage&#39;</span><span class="p">],</span> <span class="mi">0</span><span class="p">)</span>
  2169. <span class="n">eggs</span> <span class="o">=</span> <span class="n">_temp</span><span class="o">.</span><span class="n">eggs</span>
  2170. <span class="n">saus</span> <span class="o">=</span> <span class="n">_temp</span><span class="o">.</span><span class="n">sausage</span>
  2171. </pre></div>
  2172. </div>
  2173. <p>Here, the <code class="docutils literal notranslate"><span class="pre">spam.ham</span></code> module is returned from <a class="reference internal" href="#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a>. From this
  2174. object, the names to import are retrieved and assigned to their respective
  2175. names.</p>
  2176. <p>If you simply want to import a module (potentially within a package) by name,
  2177. use <a class="reference internal" href="importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a>.</p>
  2178. <div class="versionchanged">
  2179. <p><span class="versionmodified changed">Changed in version 3.3: </span>Negative values for <em>level</em> are no longer supported (which also changes
  2180. the default value to 0).</p>
  2181. </div>
  2182. <div class="versionchanged">
  2183. <p><span class="versionmodified changed">Changed in version 3.9: </span>When the command line options <a class="reference internal" href="../using/cmdline.html#cmdoption-E"><code class="xref std std-option docutils literal notranslate"><span class="pre">-E</span></code></a> or <a class="reference internal" href="../using/cmdline.html#cmdoption-I"><code class="xref std std-option docutils literal notranslate"><span class="pre">-I</span></code></a> are being used,
  2184. the environment variable <span class="target" id="index-16"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONCASEOK"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONCASEOK</span></code></a> is now ignored.</p>
  2185. </div>
  2186. </dd></dl>
  2187. <p class="rubric">Footnotes</p>
  2188. <aside class="footnote-list brackets">
  2189. <aside class="footnote brackets" id="id2" role="doc-footnote">
  2190. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
  2191. <p>Note that the parser only accepts the Unix-style end of line convention.
  2192. If you are reading the code from a file, make sure to use newline conversion
  2193. mode to convert Windows or Mac-style newlines.</p>
  2194. </aside>
  2195. </aside>
  2196. </section>
  2197. <div class="clearer"></div>
  2198. </div>
  2199. </div>
  2200. </div>
  2201. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  2202. <div class="sphinxsidebarwrapper">
  2203. <div>
  2204. <h4>Previous topic</h4>
  2205. <p class="topless"><a href="intro.html"
  2206. title="previous chapter">Introduction</a></p>
  2207. </div>
  2208. <div>
  2209. <h4>Next topic</h4>
  2210. <p class="topless"><a href="constants.html"
  2211. title="next chapter">Built-in Constants</a></p>
  2212. </div>
  2213. <div role="note" aria-label="source link">
  2214. <h3>This Page</h3>
  2215. <ul class="this-page-menu">
  2216. <li><a href="../bugs.html">Report a Bug</a></li>
  2217. <li>
  2218. <a href="https://github.com/python/cpython/blob/main/Doc/library/functions.rst"
  2219. rel="nofollow">Show Source
  2220. </a>
  2221. </li>
  2222. </ul>
  2223. </div>
  2224. </div>
  2225. <div id="sidebarbutton" title="Collapse sidebar">
  2226. <span>«</span>
  2227. </div>
  2228. </div>
  2229. <div class="clearer"></div>
  2230. </div>
  2231. <div class="related" role="navigation" aria-label="related navigation">
  2232. <h3>Navigation</h3>
  2233. <ul>
  2234. <li class="right" style="margin-right: 10px">
  2235. <a href="../genindex.html" title="General Index"
  2236. >index</a></li>
  2237. <li class="right" >
  2238. <a href="../py-modindex.html" title="Python Module Index"
  2239. >modules</a> |</li>
  2240. <li class="right" >
  2241. <a href="constants.html" title="Built-in Constants"
  2242. >next</a> |</li>
  2243. <li class="right" >
  2244. <a href="intro.html" title="Introduction"
  2245. >previous</a> |</li>
  2246. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  2247. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  2248. <li class="switchers">
  2249. <div class="language_switcher_placeholder"></div>
  2250. <div class="version_switcher_placeholder"></div>
  2251. </li>
  2252. <li>
  2253. </li>
  2254. <li id="cpython-language-and-version">
  2255. <a href="../index.html">3.12.3 Documentation</a> &#187;
  2256. </li>
  2257. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  2258. <li class="nav-item nav-item-this"><a href="">Built-in Functions</a></li>
  2259. <li class="right">
  2260. <div class="inline-search" role="search">
  2261. <form class="inline-search" action="../search.html" method="get">
  2262. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  2263. <input type="submit" value="Go" />
  2264. </form>
  2265. </div>
  2266. |
  2267. </li>
  2268. <li class="right">
  2269. <label class="theme-selector-label">
  2270. Theme
  2271. <select class="theme-selector" oninput="activateTheme(this.value)">
  2272. <option value="auto" selected>Auto</option>
  2273. <option value="light">Light</option>
  2274. <option value="dark">Dark</option>
  2275. </select>
  2276. </label> |</li>
  2277. </ul>
  2278. </div>
  2279. <div class="footer">
  2280. &copy;
  2281. <a href="../copyright.html">
  2282. Copyright
  2283. </a>
  2284. 2001-2024, Python Software Foundation.
  2285. <br />
  2286. This page is licensed under the Python Software Foundation License Version 2.
  2287. <br />
  2288. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2289. <br />
  2290. See <a href="/license.html">History and License</a> for more information.<br />
  2291. <br />
  2292. The Python Software Foundation is a non-profit corporation.
  2293. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2294. <br />
  2295. <br />
  2296. Last updated on Apr 09, 2024 (13:47 UTC).
  2297. <a href="/bugs.html">Found a bug</a>?
  2298. <br />
  2299. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  2300. </div>
  2301. </body>
  2302. </html>
上海开阖软件有限公司 沪ICP备12045867号-1