0x1949 Team - FAZEMRX - MANAGER
Edit File: functions.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" /> <title>2. Built-in Functions — Python 2.7.18 documentation</title> <link rel="stylesheet" type="text/css" href="../_static/pygments.css" /> <link rel="stylesheet" type="text/css" href="../_static/classic.css" /> <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script> <script src="../_static/jquery.js"></script> <script src="../_static/underscore.js"></script> <script src="../_static/doctools.js"></script> <script src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.18 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="4. Built-in Constants" href="constants.html" /> <link rel="prev" title="1. Introduction" href="intro.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="file:///usr/share/doc/python2.7/html/library/functions.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;"> This document is for an old version of Python that is <a href="https://devguide.python.org/devcycle/#end-of-life-branches">no longer supported</a>. You should install the python3 and python3-doc packages and read the <a href="file:///usr/share/doc/python3-doc/html/library/functions.html"> Python documentation for the Python3 version packaged in this release</a>. </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="constants.html" title="4. Built-in Constants" accesskey="N">next</a> |</li> <li class="right" > <a href="intro.html" title="1. Introduction" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.18 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Standard Library</a> »</li> <li class="nav-item nav-item-this"><a href=""><span class="section-number">2. </span>Built-in Functions</a></li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <section id="built-in-functions"> <span id="built-in-funcs"></span><h1><span class="section-number">2. </span>Built-in Functions<a class="headerlink" href="#built-in-functions" title="Permalink to this headline">¶</a></h1> <p>The Python interpreter has a number of functions built into it that are always available. They are listed here in alphabetical order.</p> <table class="docutils align-default"> <colgroup> <col style="width: 21%" /> <col style="width: 19%" /> <col style="width: 20%" /> <col style="width: 19%" /> <col style="width: 22%" /> </colgroup> <thead> <tr class="row-odd"><th class="head"></th> <th class="head"></th> <th class="head"><p>Built-in Functions</p></th> <th class="head"></th> <th class="head"></th> </tr> </thead> <tbody> <tr class="row-even"><td><p><a class="reference internal" href="#abs" title="abs"><code class="xref py py-func docutils literal notranslate"><span class="pre">abs()</span></code></a></p></td> <td><p><a class="reference internal" href="#divmod" title="divmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">divmod()</span></code></a></p></td> <td><p><a class="reference internal" href="#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a></p></td> <td><p><a class="reference internal" href="#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a></p></td> <td><p><a class="reference internal" href="#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a></p></td> </tr> <tr class="row-odd"><td><p><a class="reference internal" href="#all" title="all"><code class="xref py py-func docutils literal notranslate"><span class="pre">all()</span></code></a></p></td> <td><p><a class="reference internal" href="#enumerate" title="enumerate"><code class="xref py py-func docutils literal notranslate"><span class="pre">enumerate()</span></code></a></p></td> <td><p><a class="reference internal" href="#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a></p></td> <td><p><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></td> <td><p><a class="reference internal" href="#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a></p></td> </tr> <tr class="row-even"><td><p><a class="reference internal" href="#any" title="any"><code class="xref py py-func docutils literal notranslate"><span class="pre">any()</span></code></a></p></td> <td><p><a class="reference internal" href="#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a></p></td> <td><p><a class="reference internal" href="#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a></p></td> <td><p><a class="reference internal" href="#pow" title="pow"><code class="xref py py-func docutils literal notranslate"><span class="pre">pow()</span></code></a></p></td> <td><p><a class="reference internal" href="#sum" title="sum"><code class="xref py py-func docutils literal notranslate"><span class="pre">sum()</span></code></a></p></td> </tr> <tr class="row-odd"><td><p><a class="reference internal" href="#basestring" title="basestring"><code class="xref py py-func docutils literal notranslate"><span class="pre">basestring()</span></code></a></p></td> <td><p><a class="reference internal" href="#execfile" title="execfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">execfile()</span></code></a></p></td> <td><p><a class="reference internal" href="#issubclass" title="issubclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubclass()</span></code></a></p></td> <td><p><a class="reference internal" href="#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a></p></td> <td><p><a class="reference internal" href="#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a></p></td> </tr> <tr class="row-even"><td><p><a class="reference internal" href="#bin" title="bin"><code class="xref py py-func docutils literal notranslate"><span class="pre">bin()</span></code></a></p></td> <td><p><a class="reference internal" href="#file" title="file"><code class="xref py py-func docutils literal notranslate"><span class="pre">file()</span></code></a></p></td> <td><p><a class="reference internal" href="#iter" title="iter"><code class="xref py py-func docutils literal notranslate"><span class="pre">iter()</span></code></a></p></td> <td><p><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></td> <td><p><a class="reference internal" href="#tuple" title="tuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">tuple()</span></code></a></p></td> </tr> <tr class="row-odd"><td><p><a class="reference internal" href="#bool" title="bool"><code class="xref py py-func docutils literal notranslate"><span class="pre">bool()</span></code></a></p></td> <td><p><a class="reference internal" href="#filter" title="filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">filter()</span></code></a></p></td> <td><p><a class="reference internal" href="#len" title="len"><code class="xref py py-func docutils literal notranslate"><span class="pre">len()</span></code></a></p></td> <td><p><a class="reference internal" href="#range" title="range"><code class="xref py py-func docutils literal notranslate"><span class="pre">range()</span></code></a></p></td> <td><p><a class="reference internal" href="#type" title="type"><code class="xref py py-func docutils literal notranslate"><span class="pre">type()</span></code></a></p></td> </tr> <tr class="row-even"><td><p><a class="reference internal" href="#bytearray" title="bytearray"><code class="xref py py-func docutils literal notranslate"><span class="pre">bytearray()</span></code></a></p></td> <td><p><a class="reference internal" href="#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a></p></td> <td><p><a class="reference internal" href="#func-list"><code class="docutils literal notranslate"><span class="pre">list()</span></code></a></p></td> <td><p><a class="reference internal" href="#raw_input" title="raw_input"><code class="xref py py-func docutils literal notranslate"><span class="pre">raw_input()</span></code></a></p></td> <td><p><a class="reference internal" href="#unichr" title="unichr"><code class="xref py py-func docutils literal notranslate"><span class="pre">unichr()</span></code></a></p></td> </tr> <tr class="row-odd"><td><p><a class="reference internal" href="#callable" title="callable"><code class="xref py py-func docutils literal notranslate"><span class="pre">callable()</span></code></a></p></td> <td><p><a class="reference internal" href="#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a></p></td> <td><p><a class="reference internal" href="#locals" title="locals"><code class="xref py py-func docutils literal notranslate"><span class="pre">locals()</span></code></a></p></td> <td><p><a class="reference internal" href="#reduce" title="reduce"><code class="xref py py-func docutils literal notranslate"><span class="pre">reduce()</span></code></a></p></td> <td><p><a class="reference internal" href="#unicode" title="unicode"><code class="xref py py-func docutils literal notranslate"><span class="pre">unicode()</span></code></a></p></td> </tr> <tr class="row-even"><td><p><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></td> <td><p><a class="reference internal" href="#func-frozenset"><code class="docutils literal notranslate"><span class="pre">frozenset()</span></code></a></p></td> <td><p><a class="reference internal" href="#long" title="long"><code class="xref py py-func docutils literal notranslate"><span class="pre">long()</span></code></a></p></td> <td><p><a class="reference internal" href="#reload" title="reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code></a></p></td> <td><p><a class="reference internal" href="#vars" title="vars"><code class="xref py py-func docutils literal notranslate"><span class="pre">vars()</span></code></a></p></td> </tr> <tr class="row-odd"><td><p><a class="reference internal" href="#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a></p></td> <td><p><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></td> <td><p><a class="reference internal" href="#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a></p></td> <td><p><a class="reference internal" href="#func-repr"><code class="docutils literal notranslate"><span class="pre">repr()</span></code></a></p></td> <td><p><a class="reference internal" href="#xrange" title="xrange"><code class="xref py py-func docutils literal notranslate"><span class="pre">xrange()</span></code></a></p></td> </tr> <tr class="row-even"><td><p><a class="reference internal" href="#cmp" title="cmp"><code class="xref py py-func docutils literal notranslate"><span class="pre">cmp()</span></code></a></p></td> <td><p><a class="reference internal" href="#globals" title="globals"><code class="xref py py-func docutils literal notranslate"><span class="pre">globals()</span></code></a></p></td> <td><p><a class="reference internal" href="#max" title="max"><code class="xref py py-func docutils literal notranslate"><span class="pre">max()</span></code></a></p></td> <td><p><a class="reference internal" href="#reversed" title="reversed"><code class="xref py py-func docutils literal notranslate"><span class="pre">reversed()</span></code></a></p></td> <td><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></p></td> </tr> <tr class="row-odd"><td><p><a class="reference internal" href="#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a></p></td> <td><p><a class="reference internal" href="#hasattr" title="hasattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">hasattr()</span></code></a></p></td> <td><p><a class="reference internal" href="#func-memoryview"><code class="docutils literal notranslate"><span class="pre">memoryview()</span></code></a></p></td> <td><p><a class="reference internal" href="#round" title="round"><code class="xref py py-func docutils literal notranslate"><span class="pre">round()</span></code></a></p></td> <td><p><a class="reference internal" href="#import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a></p></td> </tr> <tr class="row-even"><td><p><a class="reference internal" href="#complex" title="complex"><code class="xref py py-func docutils literal notranslate"><span class="pre">complex()</span></code></a></p></td> <td><p><a class="reference internal" href="#hash" title="hash"><code class="xref py py-func docutils literal notranslate"><span class="pre">hash()</span></code></a></p></td> <td><p><a class="reference internal" href="#min" title="min"><code class="xref py py-func docutils literal notranslate"><span class="pre">min()</span></code></a></p></td> <td><p><a class="reference internal" href="#func-set"><code class="docutils literal notranslate"><span class="pre">set()</span></code></a></p></td> <td></td> </tr> <tr class="row-odd"><td><p><a class="reference internal" href="#delattr" title="delattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">delattr()</span></code></a></p></td> <td><p><a class="reference internal" href="#help" title="help"><code class="xref py py-func docutils literal notranslate"><span class="pre">help()</span></code></a></p></td> <td><p><a class="reference internal" href="#next" title="next"><code class="xref py py-func docutils literal notranslate"><span class="pre">next()</span></code></a></p></td> <td><p><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></td> <td></td> </tr> <tr class="row-even"><td><p><a class="reference internal" href="#func-dict"><code class="docutils literal notranslate"><span class="pre">dict()</span></code></a></p></td> <td><p><a class="reference internal" href="#hex" title="hex"><code class="xref py py-func docutils literal notranslate"><span class="pre">hex()</span></code></a></p></td> <td><p><a class="reference internal" href="#object" title="object"><code class="xref py py-func docutils literal notranslate"><span class="pre">object()</span></code></a></p></td> <td><p><a class="reference internal" href="#slice" title="slice"><code class="xref py py-func docutils literal notranslate"><span class="pre">slice()</span></code></a></p></td> <td></td> </tr> <tr class="row-odd"><td><p><a class="reference internal" href="#dir" title="dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code></a></p></td> <td><p><a class="reference internal" href="#id" title="id"><code class="xref py py-func docutils literal notranslate"><span class="pre">id()</span></code></a></p></td> <td><p><a class="reference internal" href="#oct" title="oct"><code class="xref py py-func docutils literal notranslate"><span class="pre">oct()</span></code></a></p></td> <td><p><a class="reference internal" href="#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a></p></td> <td></td> </tr> </tbody> </table> <p>In addition, there are other four built-in functions that are no longer considered essential: <a class="reference internal" href="#apply" title="apply"><code class="xref py py-func docutils literal notranslate"><span class="pre">apply()</span></code></a>, <a class="reference internal" href="#buffer" title="buffer"><code class="xref py py-func docutils literal notranslate"><span class="pre">buffer()</span></code></a>, <a class="reference internal" href="#coerce" title="coerce"><code class="xref py py-func docutils literal notranslate"><span class="pre">coerce()</span></code></a>, and <a class="reference internal" href="#intern" title="intern"><code class="xref py py-func docutils literal notranslate"><span class="pre">intern()</span></code></a>. They are documented in the <a class="reference internal" href="#non-essential-built-in-funcs"><span class="std std-ref">Non-essential Built-in Functions</span></a> section.</p> <dl class="py function"> <dt class="sig sig-object py" id="abs"> <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="Permalink to this definition">¶</a></dt> <dd><p>Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a complex number, its magnitude is returned.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="all"> <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="Permalink to this definition">¶</a></dt> <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 is empty). Equivalent to:</p> <div class="highlight-default 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> <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">element</span><span class="p">:</span> <span class="k">return</span> <span class="kc">False</span> <span class="k">return</span> <span class="kc">True</span> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.5.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="any"> <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="Permalink to this definition">¶</a></dt> <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 is empty, return <code class="docutils literal notranslate"><span class="pre">False</span></code>. Equivalent to:</p> <div class="highlight-default 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> <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> <span class="k">if</span> <span class="n">element</span><span class="p">:</span> <span class="k">return</span> <span class="kc">True</span> <span class="k">return</span> <span class="kc">False</span> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.5.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="basestring"> <span class="sig-name descname"><span class="pre">basestring</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#basestring" title="Permalink to this definition">¶</a></dt> <dd><p>This abstract type is the superclass for <a class="reference internal" href="#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> and <a class="reference internal" href="#unicode" title="unicode"><code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code></a>. It cannot be called or instantiated, but it can be used to test whether an object is an instance of <a class="reference internal" href="#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or <a class="reference internal" href="#unicode" title="unicode"><code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code></a>. <code class="docutils literal notranslate"><span class="pre">isinstance(obj,</span> <span class="pre">basestring)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">isinstance(obj,</span> <span class="pre">(str,</span> <span class="pre">unicode))</span></code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.3.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="bin"> <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="Permalink to this definition">¶</a></dt> <dd><p>Convert an integer number to a binary string. The result 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 has to define an <code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code> method that returns an integer.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.6.</span></p> </div> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="bool"> <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><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#bool" title="Permalink to this definition">¶</a></dt> <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 using the standard truth testing procedure. If <em>x</em> is false or omitted, this returns <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>; otherwise it returns <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>. <a class="reference internal" href="#bool" title="bool"><code class="xref py py-class docutils literal notranslate"><span class="pre">bool</span></code></a> is also a class, which 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>. Class <a class="reference internal" href="#bool" title="bool"><code class="xref py py-class docutils literal notranslate"><span class="pre">bool</span></code></a> cannot be subclassed further. Its only instances are <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> and <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>.</p> <div class="versionadded" id="index-0"> <p><span class="versionmodified added">New in version 2.2.1.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.3: </span>If no argument is given, this function returns <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>.</p> </div> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="bytearray"> <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><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#bytearray" title="Permalink to this definition">¶</a></dt> <dd><p>Return a new array of bytes. The <a class="reference internal" href="#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual 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 as most methods that the <a class="reference internal" href="#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> type has, see <a class="reference internal" href="stdtypes.html#string-methods"><span class="std std-ref">String Methods</span></a>.</p> <p>The optional <em>source</em> parameter can be used to initialize the array in a few different ways:</p> <ul class="simple"> <li><p>If it is <em>unicode</em>, you must also give the <em>encoding</em> (and optionally, <em>errors</em>) parameters; <a class="reference internal" href="#bytearray" title="bytearray"><code class="xref py py-func docutils literal notranslate"><span class="pre">bytearray()</span></code></a> then converts the unicode to bytes using <code class="xref py py-meth docutils literal notranslate"><span class="pre">unicode.encode()</span></code>.</p></li> <li><p>If it is an <em>integer</em>, the array will have that size and will be initialized with null bytes.</p></li> <li><p>If it is an object conforming to the <em>buffer</em> interface, a read-only buffer of the object will be used to initialize the bytes array.</p></li> <li><p>If it is an <em>iterable</em>, it must be an iterable of integers in the range <code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">x</span> <span class="pre"><</span> <span class="pre">256</span></code>, which are used as the initial contents of the array.</p></li> </ul> <p>Without an argument, an array of size 0 is created.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.6.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="callable"> <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="Permalink to this definition">¶</a></dt> <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, <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 true, it is still possible that a call fails, but if it is false, calling <em>object</em> will never succeed. Note that classes are callable (calling a class returns a new instance); class instances are callable if they have a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__call__()</span></code> method.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="chr"> <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="Permalink to this definition">¶</a></dt> <dd><p>Return a string of one character whose ASCII code is the 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>. 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>. The argument must be in the range [0..255], inclusive; <code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code> will be raised if <em>i</em> is outside that range. See also <a class="reference internal" href="#unichr" title="unichr"><code class="xref py py-func docutils literal notranslate"><span class="pre">unichr()</span></code></a>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="classmethod"> <span class="sig-name descname"><span class="pre">classmethod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">function</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#classmethod" title="Permalink to this definition">¶</a></dt> <dd><p>Return a class method for <em>function</em>.</p> <p>A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="nd">@classmethod</span> <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><span class="p">):</span> <span class="o">...</span> </pre></div> </div> <p>The <code class="docutils literal notranslate"><span class="pre">@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 <a class="reference internal" href="../reference/compound_stmts.html#function"><span class="std std-ref">Function definitions</span></a> for details.</p> <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 as <code class="docutils literal notranslate"><span class="pre">C().f()</span></code>). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.</p> <p>Class methods are different than C++ or Java static methods. If you want those, 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>.</p> <p>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> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.4: </span>Function decorator syntax added.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="cmp"> <span class="sig-name descname"><span class="pre">cmp</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">y</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmp" title="Permalink to this definition">¶</a></dt> <dd><p>Compare the two objects <em>x</em> and <em>y</em> and return an integer according to the outcome. The return value is negative if <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre"><</span> <span class="pre">y</span></code>, zero if <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y</span></code> and strictly positive if <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">></span> <span class="pre">y</span></code>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="compile"> <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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">dont_inherit</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#compile" title="Permalink to this definition">¶</a></dt> <dd><p>Compile the <em>source</em> into a code or AST object. Code objects can be executed by an <a class="reference internal" href="../reference/simple_stmts.html#exec"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">exec</span></code></a> statement or evaluated by a call 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>. <em>source</em> can either be a Unicode string, a <em>Latin-1</em> encoded 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 for information on how to work with AST objects.</p> <p>The <em>filename</em> argument should give the file from which the code was read; pass some recognizable value if it wasn’t read from a file (<code class="docutils literal notranslate"><span class="pre">'<string>'</span></code> is commonly used).</p> <p>The <em>mode</em> argument specifies what kind of code must be compiled; it can be <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 consists of a single expression, or <code class="docutils literal notranslate"><span class="pre">'single'</span></code> if it consists of a single interactive statement (in the latter case, expression statements that evaluate to something other than <code class="docutils literal notranslate"><span class="pre">None</span></code> will be printed).</p> <p>The optional arguments <em>flags</em> and <em>dont_inherit</em> control which future statements (see <span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0236"><strong>PEP 236</strong></a>) affect the compilation of <em>source</em>. If neither is present (or both are zero) the code is compiled with those future statements that are in effect in 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> argument is given and <em>dont_inherit</em> is not (or is zero) then the future statements specified by the <em>flags</em> argument are used in addition to those that would be used anyway. If <em>dont_inherit</em> is a non-zero integer then the <em>flags</em> argument is it – the future statements in effect around the call to compile are ignored.</p> <p>Future statements are specified by bits which can be bitwise ORed together to specify multiple statements. The bitfield required to specify a given feature can be found as the <code class="xref py py-attr docutils literal notranslate"><span class="pre">compiler_flag</span></code> attribute on the <code class="xref py py-class docutils literal notranslate"><span class="pre">_Feature</span></code> 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.</p> <p>This function raises <code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code> if the compiled source is invalid, and <code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code> if the source contains null bytes.</p> <p>If you want to parse Python code into its AST representation, see <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> <div class="admonition note"> <p class="admonition-title">Note</p> <p>When compiling a string with multi-line code in <code class="docutils literal notranslate"><span class="pre">'single'</span></code> or <code class="docutils literal notranslate"><span class="pre">'eval'</span></code> mode, input must be terminated by at least one newline character. This is to facilitate detection of incomplete and complete 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> </div> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>It is possible to crash the Python interpreter with a sufficiently large/complex string when compiling to an AST object due to stack depth limitations in Python’s AST compiler.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.3: </span>The <em>flags</em> and <em>dont_inherit</em> arguments were added.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.6: </span>Support for compiling AST objects.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.7: </span>Allowed use of Windows and Mac newlines. Also input in <code class="docutils literal notranslate"><span class="pre">'exec'</span></code> mode does not have to end in a newline anymore.</p> </div> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="complex"> <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><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">real</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">imag</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#complex" title="Permalink to this definition">¶</a></dt> <dd><p>Return a complex number with the value <em>real</em> + <em>imag</em>*1j or convert a string or number to a complex number. If the first parameter is a string, it will be interpreted as a complex number and the function must be called without a second parameter. The second parameter can never be a string. Each argument may be any numeric type (including complex). If <em>imag</em> is omitted, it defaults to zero and the function serves as a numeric conversion function like <a class="reference internal" href="#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a>, <a class="reference internal" href="#long" title="long"><code class="xref py py-func docutils literal notranslate"><span class="pre">long()</span></code></a> and <a class="reference internal" href="#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a>. If both arguments are omitted, returns <code class="docutils literal notranslate"><span class="pre">0j</span></code>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>When converting from a string, the string must not contain whitespace 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, <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 <code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code>.</p> </div> <p>The complex type is described in <a class="reference internal" href="stdtypes.html#typesnumeric"><span class="std std-ref">Numeric Types — int, float, long, complex</span></a>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="delattr"> <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="Permalink to this definition">¶</a></dt> <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 string. The string must be the name of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For 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>.</p> </dd></dl> <span class="target" id="func-dict"></span><dl class="py class"> <dt class="sig sig-object py"> <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> <dt class="sig sig-object py"> <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> <dt class="sig sig-object py"> <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> <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. 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> <p>For other containers see the built-in <code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code>, <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 <a class="reference internal" href="#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: High-performance datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="dir"> <span class="sig-name descname"><span class="pre">dir</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#dir" title="Permalink to this definition">¶</a></dt> <dd><p>Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.</p> <p>If the object has a method named <code class="xref py py-meth docutils literal notranslate"><span class="pre">__dir__()</span></code>, this method will be called and must return the list of attributes. This allows objects that implement a custom <code class="xref py py-func docutils literal notranslate"><span class="pre">__getattr__()</span></code> or <code class="xref py py-func docutils literal notranslate"><span class="pre">__getattribute__()</span></code> function to customize the way <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> <p>If the object does not provide <code class="xref py py-meth docutils literal notranslate"><span class="pre">__dir__()</span></code>, the function tries its best to gather information from the object’s <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 from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom <code class="xref py py-func docutils literal notranslate"><span class="pre">__getattr__()</span></code>.</p> <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 objects, as it attempts to produce the most relevant, rather than complete, information:</p> <ul class="simple"> <li><p>If the object is a module object, the list contains the names of the module’s attributes.</p></li> <li><p>If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.</p></li> <li><p>Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.</p></li> </ul> <p>The resulting list is sorted alphabetically. For example:</p> <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">struct</span> <span class="gp">>>> </span><span class="nb">dir</span><span class="p">()</span> <span class="c1"># show the names in the module namespace</span> <span class="go">['__builtins__', '__doc__', '__name__', 'struct']</span> <span class="gp">>>> </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> <span class="go">['Struct', '__builtins__', '__doc__', '__file__', '__name__',</span> <span class="go"> '__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',</span> <span class="go"> 'unpack', 'unpack_from']</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">Shape</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="go"> def __dir__(self):</span> <span class="go"> return ['area', 'perimeter', 'location']</span> <span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">Shape</span><span class="p">()</span> <span class="gp">>>> </span><span class="nb">dir</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="go">['area', 'perimeter', 'location']</span> </pre></div> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <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 interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the argument is a class.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="divmod"> <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="Permalink to this definition">¶</a></dt> <dd><p>Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using long division. With mixed operand types, the rules for binary arithmetic operators apply. For plain and long 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 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> <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 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> <span class="pre"><=</span> <span class="pre">abs(a</span> <span class="pre">%</span> <span class="pre">b)</span> <span class="pre"><</span> <span class="pre">abs(b)</span></code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.3: </span>Using <a class="reference internal" href="#divmod" title="divmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">divmod()</span></code></a> with complex numbers is deprecated.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="enumerate"> <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">sequence</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="Permalink to this definition">¶</a></dt> <dd><p>Return an enumerate object. <em>sequence</em> must be a sequence, an <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. The <code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code> method of the iterator returned by <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 defaults to 0) and the values obtained from iterating over <em>sequence</em>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">seasons</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'Spring'</span><span class="p">,</span> <span class="s1">'Summer'</span><span class="p">,</span> <span class="s1">'Fall'</span><span class="p">,</span> <span class="s1">'Winter'</span><span class="p">]</span> <span class="gp">>>> </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="go">[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]</span> <span class="gp">>>> </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> <span class="go">[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]</span> </pre></div> </div> <p>Equivalent to:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">enumerate</span><span class="p">(</span><span class="n">sequence</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span> <span class="n">n</span> <span class="o">=</span> <span class="n">start</span> <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">sequence</span><span class="p">:</span> <span class="k">yield</span> <span class="n">n</span><span class="p">,</span> <span class="n">elem</span> <span class="n">n</span> <span class="o">+=</span> <span class="mi">1</span> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.6: </span>The <em>start</em> parameter was added.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="eval"> <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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">globals</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">locals</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#eval" title="Permalink to this definition">¶</a></dt> <dd><p>The arguments are a Unicode or <em>Latin-1</em> encoded string and optional globals and locals. If provided, <em>globals</em> must be a dictionary. If provided, <em>locals</em> can be any mapping object.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.4: </span>formerly <em>locals</em> was required to be a dictionary.</p> </div> <p>The <em>expression</em> argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the <em>globals</em> and <em>locals</em> dictionaries as global and local namespace. If the <em>globals</em> dictionary is present and lacks ‘__builtins__’, the current globals are copied into <em>globals</em> before <em>expression</em> is parsed. This means that <em>expression</em> normally has full access to the standard <a class="reference internal" href="__builtin__.html#module-__builtin__" title="__builtin__: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__builtin__</span></code></a> module and restricted environments are propagated. If the <em>locals</em> dictionary is omitted it defaults to the <em>globals</em> dictionary. If both dictionaries are omitted, the expression is executed in the environment where <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. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions. Example:</p> <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span> <span class="gp">>>> </span><span class="nb">print</span> <span class="nb">eval</span><span class="p">(</span><span class="s1">'x+1'</span><span class="p">)</span> <span class="go">2</span> </pre></div> </div> <p>This function can also be used to execute arbitrary code objects (such as 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 of a string. If the code object has been compiled with <code class="docutils literal notranslate"><span class="pre">'exec'</span></code> as the <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> <p>Hints: dynamic execution of statements is supported by the <a class="reference internal" href="../reference/simple_stmts.html#exec"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">exec</span></code></a> statement. Execution of statements from a file is supported by the <a class="reference internal" href="#execfile" title="execfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">execfile()</span></code></a> 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 returns the current global and local dictionary, respectively, which may be 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="#execfile" title="execfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">execfile()</span></code></a>.</p> <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 with expressions containing only literals.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="execfile"> <span class="sig-name descname"><span class="pre">execfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">globals</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">locals</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#execfile" title="Permalink to this definition">¶</a></dt> <dd><p>This function is similar to the <a class="reference internal" href="../reference/simple_stmts.html#exec"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">exec</span></code></a> statement, but parses a file instead of a string. It is different from 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 in that it does not use the module administration — it reads the file unconditionally and does not create a new module. <a class="footnote-reference brackets" href="#id3" id="id1">1</a></p> <p>The arguments are a file name and two optional dictionaries. The file is parsed and evaluated as a sequence of Python statements (similarly to a module) using the <em>globals</em> and <em>locals</em> dictionaries as global and local namespace. If provided, <em>locals</em> can be any mapping object. Remember that at module level, globals and locals are the same dictionary. If two separate objects are passed as <em>globals</em> and <em>locals</em>, the code will be executed as if it were embedded in a class definition.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.4: </span>formerly <em>locals</em> was required to be a dictionary.</p> </div> <p>If the <em>locals</em> dictionary is omitted it defaults to the <em>globals</em> dictionary. If both dictionaries are omitted, the expression is executed in the environment where <a class="reference internal" href="#execfile" title="execfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">execfile()</span></code></a> is called. The return value is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <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: modifications to the default <em>locals</em> dictionary should not be attempted. Pass an explicit <em>locals</em> dictionary if you need to see effects of the code on <em>locals</em> after function <a class="reference internal" href="#execfile" title="execfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">execfile()</span></code></a> returns. <a class="reference internal" href="#execfile" title="execfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">execfile()</span></code></a> cannot be used reliably to modify a function’s locals.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="file"> <span class="sig-name descname"><span class="pre">file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">buffering</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#file" title="Permalink to this definition">¶</a></dt> <dd><p>Constructor function for the <a class="reference internal" href="#file" title="file"><code class="xref py py-class docutils literal notranslate"><span class="pre">file</span></code></a> type, described further in section <a class="reference internal" href="stdtypes.html#bltin-file-objects"><span class="std std-ref">File Objects</span></a>. The constructor’s arguments are the same as those of 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> built-in function described below.</p> <p>When opening a file, it’s preferable to use <a class="reference internal" href="#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> instead of invoking this constructor directly. <a class="reference internal" href="#file" title="file"><code class="xref py py-class docutils literal notranslate"><span class="pre">file</span></code></a> is more suited to type testing (for example, writing <code class="docutils literal notranslate"><span class="pre">isinstance(f,</span> <span class="pre">file)</span></code>).</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.2.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="filter"> <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="Permalink to this definition">¶</a></dt> <dd><p>Construct a list from those elements of <em>iterable</em> for which <em>function</em> returns true. <em>iterable</em> may be either a sequence, a container which supports iteration, or an iterator. If <em>iterable</em> is a string or a tuple, the result also has that type; otherwise it is always a list. If <em>function</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the identity function is assumed, that is, all elements of <em>iterable</em> that are false are removed.</p> <p>Note that <code class="docutils literal notranslate"><span class="pre">filter(function,</span> <span class="pre">iterable)</span></code> is equivalent to <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 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 <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> <p>See <a class="reference internal" href="itertools.html#itertools.ifilter" title="itertools.ifilter"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.ifilter()</span></code></a> and <a class="reference internal" href="itertools.html#itertools.ifilterfalse" title="itertools.ifilterfalse"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.ifilterfalse()</span></code></a> for iterator versions of this function, including a variation that filters for elements where the <em>function</em> returns false.</p> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="float"> <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><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#float" title="Permalink to this definition">¶</a></dt> <dd><p>Return a floating point number constructed from a number or string <em>x</em>.</p> <p>If the argument is a string, it must contain a possibly signed decimal or floating point number, possibly embedded in whitespace. The argument may also be [+|-]nan or [+|-]inf. Otherwise, the argument may be a plain or long integer or a floating point number, and a floating point number with the same value (within Python’s floating point precision) is returned. If no argument is given, returns <code class="docutils literal notranslate"><span class="pre">0.0</span></code>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p id="index-2">When passing in a string, values for NaN and Infinity may be returned, depending on the underlying C library. Float accepts the strings nan, inf and -inf for NaN and positive or negative infinity. The case and a leading + are ignored as well as a leading - is ignored for NaN. Float always represents NaN and infinity as nan, inf or -inf.</p> </div> <p>The float type is described in <a class="reference internal" href="stdtypes.html#typesnumeric"><span class="std std-ref">Numeric Types — int, float, long, complex</span></a>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="format"> <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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">format_spec</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#format" title="Permalink to this definition">¶</a></dt> <dd><p id="index-3">Convert a <em>value</em> to a “formatted” representation, as controlled by <em>format_spec</em>. The interpretation of <em>format_spec</em> will depend on the type of the <em>value</em> argument, however there is a standard formatting syntax that 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> <div class="admonition note"> <p class="admonition-title">Note</p> <p><code class="docutils literal notranslate"><span class="pre">format(value,</span> <span class="pre">format_spec)</span></code> merely calls <code class="docutils literal notranslate"><span class="pre">value.__format__(format_spec)</span></code>.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.6.</span></p> </div> </dd></dl> <span class="target" id="func-frozenset"></span><dl class="py class"> <dt class="sig sig-object py"> <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><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="optional">]</span><span class="sig-paren">)</span></dt> <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 <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 <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> <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>, <code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code>, <a class="reference internal" href="#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: High-performance datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.4.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="getattr"> <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="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">default</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#getattr" title="Permalink to this definition">¶</a></dt> <dd><p>Return the value of the named attribute of <em>object</em>. <em>name</em> must be a string. If the string is the name of one of the object’s attributes, the result is the 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 <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 provided, otherwise <code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code> is raised.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="globals"> <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="Permalink to this definition">¶</a></dt> <dd><p>Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="hasattr"> <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="Permalink to this definition">¶</a></dt> <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 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 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 raises an exception or not.)</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="hash"> <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="Permalink to this definition">¶</a></dt> <dd><p>Return the hash value of the object (if it has one). Hash values are integers. They are used to quickly compare dictionary keys during a dictionary lookup. Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0).</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="help"> <span class="sig-name descname"><span class="pre">help</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#help" title="Permalink to this definition">¶</a></dt> <dd><p>Invoke the built-in help system. (This function is intended for interactive use.) If no argument is given, the interactive help system starts on the interpreter console. If the argument is a string, then the string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed on the console. If the argument is any other kind of object, a help page on the object is generated.</p> <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> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.2.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="hex"> <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="Permalink to this definition">¶</a></dt> <dd><p>Convert an integer number (of any size) to a lowercase hexadecimal string prefixed with “0x”, for example:</p> <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">hex</span><span class="p">(</span><span class="mi">255</span><span class="p">)</span> <span class="go">'0xff'</span> <span class="gp">>>> </span><span class="nb">hex</span><span class="p">(</span><span class="o">-</span><span class="mi">42</span><span class="p">)</span> <span class="go">'-0x2a'</span> <span class="gp">>>> </span><span class="nb">hex</span><span class="p">(</span><span class="mi">1</span><span class="n">L</span><span class="p">)</span> <span class="go">'0x1L'</span> </pre></div> </div> <p>If x 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> or <a class="reference internal" href="#long" title="long"><code class="xref py py-class docutils literal notranslate"><span class="pre">long</span></code></a> object, it has to define a __hex__() method that returns a string.</p> <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 integer using a base of 16.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>To obtain a hexadecimal string representation for a float, use the <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> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.4: </span>Formerly only returned an unsigned literal.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="id"> <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="Permalink to this definition">¶</a></dt> <dd><p>Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. 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> value.</p> <div class="impl-detail compound"> <p><strong>CPython implementation detail:</strong> This is the address of the object in memory.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="input"> <span class="sig-name descname"><span class="pre">input</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">prompt</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#input" title="Permalink to this definition">¶</a></dt> <dd><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">eval(raw_input(prompt))</span></code>.</p> <p>This function does not catch user errors. If the input is not syntactically valid, a <code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code> will be raised. Other exceptions may be raised if there is an error during evaluation.</p> <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 to provide elaborate line editing and history features.</p> <p>Consider using the <a class="reference internal" href="#raw_input" title="raw_input"><code class="xref py py-func docutils literal notranslate"><span class="pre">raw_input()</span></code></a> function for general input from users.</p> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="int"> <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="Permalink to this definition">¶</a></dt> <dt class="sig sig-object py"> <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> <dd><p>Return an integer object constructed from a number or string <em>x</em>, or return <code class="docutils literal notranslate"><span class="pre">0</span></code> if no arguments are given. If <em>x</em> is a number, it can be a plain integer, a long integer, or a floating point number. If <em>x</em> is floating point, the conversion truncates towards zero. If the argument is outside the integer range, the function returns a long object instead.</p> <p>If <em>x</em> is not a number or if <em>base</em> is given, then <em>x</em> must be a string or Unicode object representing an <a class="reference internal" href="../reference/lexical_analysis.html#integers"><span class="std std-ref">integer literal</span></a> in radix <em>base</em>. Optionally, the literal 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> (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with <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>) having values 10 to 35. The default <em>base</em> is 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals 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>/<code class="docutils literal notranslate"><span class="pre">0</span></code>, or <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. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.</p> <p>The integer type is described in <a class="reference internal" href="stdtypes.html#typesnumeric"><span class="std std-ref">Numeric Types — int, float, long, complex</span></a>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="isinstance"> <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="Permalink to this definition">¶</a></dt> <dd><p>Return true if the <em>object</em> argument is an instance of the <em>classinfo</em> 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. Also return true if <em>classinfo</em> is a type object (new-style class) and <em>object</em> is an object of that type 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 a class instance or an object of the given type, the function always returns false. If <em>classinfo</em> is a tuple of class or type objects (or recursively, other such tuples), return true if <em>object</em> is an instance of any of the classes or types. If <em>classinfo</em> is not a class, type, or tuple of classes, types, and such tuples, a <code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code> exception is raised.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.2: </span>Support for a tuple of type information was added.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="issubclass"> <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="Permalink to this definition">¶</a></dt> <dd><p>Return true 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 class is considered a subclass of itself. <em>classinfo</em> may be a tuple of class objects, in which case every entry in <em>classinfo</em> will be checked. In any other case, a <code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code> exception is raised.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.3: </span>Support for a tuple of type information was added.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="iter"> <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">o</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">sentinel</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#iter" title="Permalink to this definition">¶</a></dt> <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 differently depending on the presence of the second argument. Without a second argument, <em>o</em> must be a collection object which supports the iteration protocol (the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code> method), or it must support the sequence protocol (the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code> method with integer arguments starting at <code class="docutils literal notranslate"><span class="pre">0</span></code>). If it does not support either of those protocols, <code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code> is raised. If the second argument, <em>sentinel</em>, is given, then <em>o</em> must be a callable object. The iterator created in this case will call <em>o</em> with no arguments for each call to its <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 <em>sentinel</em>, <code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code> will be raised, otherwise the value will be returned.</p> <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 read lines of a file until a certain line is reached. The following example reads a file until the <a class="reference internal" href="io.html#io.TextIOBase.readline" title="io.TextIOBase.readline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a> method returns an empty string:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'mydata.txt'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">iter</span><span class="p">(</span><span class="n">fp</span><span class="o">.</span><span class="n">readline</span><span class="p">,</span> <span class="s1">''</span><span class="p">):</span> <span class="n">process_line</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.2.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="len"> <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="Permalink to this definition">¶</a></dt> <dd><p>Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).</p> </dd></dl> <span class="target" id="func-list"></span><dl class="py class"> <dt class="sig sig-object py"> <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><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="optional">]</span><span class="sig-paren">)</span></dt> <dd><p>Return a list whose items are the same and in the same order as <em>iterable</em>’s items. <em>iterable</em> may be either a sequence, a container that supports iteration, or an iterator object. If <em>iterable</em> is already a list, a copy is made and returned, similar to <code class="docutils literal notranslate"><span class="pre">iterable[:]</span></code>. For instance, <code class="docutils literal notranslate"><span class="pre">list('abc')</span></code> returns <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></code> and <code class="docutils literal notranslate"><span class="pre">list(</span> <span class="pre">(1,</span> <span class="pre">2,</span> <span class="pre">3)</span> <span class="pre">)</span></code> returns <code class="docutils literal notranslate"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></code>. If no argument is given, returns a new empty list, <code class="docutils literal notranslate"><span class="pre">[]</span></code>.</p> <p><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code> is a mutable sequence type, as documented in <a class="reference internal" href="stdtypes.html#typesseq"><span class="std std-ref">Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange</span></a>. For other containers see the built in <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>, <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a>, and <a class="reference internal" href="#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> classes, and the <a class="reference internal" href="collections.html#module-collections" title="collections: High-performance datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="locals"> <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="Permalink to this definition">¶</a></dt> <dd><p>Update and return a dictionary representing the current local symbol table. 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 blocks, but not in class blocks.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter.</p> </div> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="long"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">long</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="#long" title="Permalink to this definition">¶</a></dt> <dt class="sig sig-object py"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">long</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> <dd><p>Return a long integer object constructed from a string or number <em>x</em>. If the argument is a string, it must contain a possibly signed number of arbitrary size, possibly embedded in whitespace. The <em>base</em> argument is interpreted in the same way as for <a class="reference internal" href="#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a>, and may only be given when <em>x</em> is a string. Otherwise, the argument may be a plain or long integer or a floating point number, and a long integer with the same value is returned. Conversion of floating point numbers to integers truncates (towards zero). If no arguments are given, returns <code class="docutils literal notranslate"><span class="pre">0L</span></code>.</p> <p>The long type is described in <a class="reference internal" href="stdtypes.html#typesnumeric"><span class="std std-ref">Numeric Types — int, float, long, complex</span></a>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="map"> <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="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#map" title="Permalink to this definition">¶</a></dt> <dd><p>Apply <em>function</em> to every item of <em>iterable</em> and return a list of the results. If additional <em>iterable</em> arguments are passed, <em>function</em> must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with <code class="docutils literal notranslate"><span class="pre">None</span></code> items. If <em>function</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the identity function is assumed; if there are multiple arguments, <a class="reference internal" href="#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a> returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The <em>iterable</em> arguments may be a sequence or any iterable object; the result is always a list.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="max"> <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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#max" title="Permalink to this definition">¶</a></dt> <dt class="sig sig-object py"> <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="n"><span class="pre">*args</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="optional">]</span><span class="sig-paren">)</span></dt> <dd><p>Return the largest item in an iterable or the largest of two or more arguments.</p> <p>If one positional argument is provided, <em>iterable</em> must be a non-empty iterable (such as a non-empty string, tuple or list). The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.</p> <p>The optional <em>key</em> argument specifies a one-argument ordering function like that used for <code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code>. The <em>key</em> argument, if supplied, must be in keyword form (for example, <code class="docutils literal notranslate"><span class="pre">max(a,b,c,key=func)</span></code>).</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.5: </span>Added support for the optional <em>key</em> argument.</p> </div> </dd></dl> <span class="target" id="func-memoryview"></span><dl class="py function"> <dt class="sig sig-object py"> <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">obj</span></span></em><span class="sig-paren">)</span></dt> <dd><p>Return a “memory view” object created from the given argument. See <a class="reference internal" href="stdtypes.html#typememoryview"><span class="std std-ref">memoryview type</span></a> for more information.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="min"> <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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#min" title="Permalink to this definition">¶</a></dt> <dt class="sig sig-object py"> <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="n"><span class="pre">*args</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="optional">]</span><span class="sig-paren">)</span></dt> <dd><p>Return the smallest item in an iterable or the smallest of two or more arguments.</p> <p>If one positional argument is provided, <em>iterable</em> must be a non-empty iterable (such as a non-empty string, tuple or list). The smallest item in the iterable is returned. If two or more positional arguments are provided, the smallest of the positional arguments is returned.</p> <p>The optional <em>key</em> argument specifies a one-argument ordering function like that used for <code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code>. The <em>key</em> argument, if supplied, must be in keyword form (for example, <code class="docutils literal notranslate"><span class="pre">min(a,b,c,key=func)</span></code>).</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.5: </span>Added support for the optional <em>key</em> argument.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="next"> <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="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">default</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#next" title="Permalink to this definition">¶</a></dt> <dd><p>Retrieve the next item from the <em>iterator</em> by calling its <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 if the iterator is exhausted, otherwise <code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code> is raised.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.6.</span></p> </div> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="object"> <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="Permalink to this definition">¶</a></dt> <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 new style classes. It has the methods that are common to all instances of new style classes.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.3: </span>This function does not accept any arguments. Formerly, it accepted arguments but ignored them.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="oct"> <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="Permalink to this definition">¶</a></dt> <dd><p>Convert an integer number (of any size) to an octal string. The result is a valid Python expression.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.4: </span>Formerly only returned an unsigned literal.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="open"> <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">name</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">buffering</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#open" title="Permalink to this definition">¶</a></dt> <dd><p>Open a file, returning an object of the <a class="reference internal" href="#file" title="file"><code class="xref py py-class docutils literal notranslate"><span class="pre">file</span></code></a> type described in section <a class="reference internal" href="stdtypes.html#bltin-file-objects"><span class="std std-ref">File Objects</span></a>. If the file cannot be opened, <code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code> is raised. When opening a file, it’s preferable to use <a class="reference internal" href="#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> instead of invoking the <a class="reference internal" href="#file" title="file"><code class="xref py py-class docutils literal notranslate"><span class="pre">file</span></code></a> constructor directly.</p> <p>The first two arguments are the same as for <code class="docutils literal notranslate"><span class="pre">stdio</span></code>’s <code class="xref c c-func docutils literal notranslate"><span class="pre">fopen()</span></code>: <em>name</em> is the file name to be opened, and <em>mode</em> is a string indicating how the file is to be opened.</p> <p>The most commonly-used values of <em>mode</em> are <code class="docutils literal notranslate"><span class="pre">'r'</span></code> for reading, <code class="docutils literal notranslate"><span class="pre">'w'</span></code> for writing (truncating the file if it already exists), and <code class="docutils literal notranslate"><span class="pre">'a'</span></code> for appending (which on <em>some</em> Unix systems means that <em>all</em> writes append to the end of the file regardless of the current seek position). If <em>mode</em> is omitted, it defaults to <code class="docutils literal notranslate"><span class="pre">'r'</span></code>. The default is to use text mode, which may convert <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append <code class="docutils literal notranslate"><span class="pre">'b'</span></code> to the <em>mode</em> value to open the file in binary mode, which will improve portability. (Appending <code class="docutils literal notranslate"><span class="pre">'b'</span></code> is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of <em>mode</em>.</p> <p id="index-4">The optional <em>buffering</em> argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size (in bytes). A negative <em>buffering</em> means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used. <a class="footnote-reference brackets" href="#id4" id="id2">2</a></p> <p>Modes <code class="docutils literal notranslate"><span class="pre">'r+'</span></code>, <code class="docutils literal notranslate"><span class="pre">'w+'</span></code> and <code class="docutils literal notranslate"><span class="pre">'a+'</span></code> open the file for updating (reading and writing); note that <code class="docutils literal notranslate"><span class="pre">'w+'</span></code> truncates the file. Append <code class="docutils literal notranslate"><span class="pre">'b'</span></code> to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the <code class="docutils literal notranslate"><span class="pre">'b'</span></code> has no effect.</p> <p id="index-5">In addition to the standard <code class="xref c c-func docutils literal notranslate"><span class="pre">fopen()</span></code> values <em>mode</em> may be <code class="docutils literal notranslate"><span class="pre">'U'</span></code> or <code class="docutils literal notranslate"><span class="pre">'rU'</span></code>. Python is usually built with <a class="reference internal" href="../glossary.html#term-universal-newlines"><span class="xref std std-term">universal newlines</span></a> support; supplying <code class="docutils literal notranslate"><span class="pre">'U'</span></code> opens the file as a text file, but lines may be terminated by any of the following: the Unix end-of-line convention <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>, the Macintosh convention <code class="docutils literal notranslate"><span class="pre">'\r'</span></code>, or the Windows convention <code class="docutils literal notranslate"><span class="pre">'\r\n'</span></code>. All of these external representations are seen as <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> by the Python program. If Python is built without universal newlines support a <em>mode</em> with <code class="docutils literal notranslate"><span class="pre">'U'</span></code> is the same as normal text mode. Note that file objects so opened also have an attribute called <code class="xref py py-attr docutils literal notranslate"><span class="pre">newlines</span></code> which has a value of <code class="docutils literal notranslate"><span class="pre">None</span></code> (if no newlines have yet been seen), <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>, <code class="docutils literal notranslate"><span class="pre">'\r'</span></code>, <code class="docutils literal notranslate"><span class="pre">'\r\n'</span></code>, or a tuple containing all the newline types seen.</p> <p>Python enforces that the mode, after stripping <code class="docutils literal notranslate"><span class="pre">'U'</span></code>, begins with <code class="docutils literal notranslate"><span class="pre">'r'</span></code>, <code class="docutils literal notranslate"><span class="pre">'w'</span></code> or <code class="docutils literal notranslate"><span class="pre">'a'</span></code>.</p> <p>Python provides many file handling modules including <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="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>, 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> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.5: </span>Restriction on first letter of mode string introduced.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="ord"> <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="Permalink to this definition">¶</a></dt> <dd><p>Given a string of length one, return an integer representing the Unicode code point of the character when the argument is a unicode object, or the value of the byte when the argument is an 8-bit string. For example, <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>, <code class="docutils literal notranslate"><span class="pre">ord(u'\u2020')</span></code> returns <code class="docutils literal notranslate"><span class="pre">8224</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> for 8-bit strings and of <a class="reference internal" href="#unichr" title="unichr"><code class="xref py py-func docutils literal notranslate"><span class="pre">unichr()</span></code></a> for unicode objects. If a unicode argument is given and Python was built with UCS2 Unicode, then the character’s code point must be in the range [0..65535] inclusive; otherwise the string length is two, and a <code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code> will be raised.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="pow"> <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">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">y</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">z</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#pow" title="Permalink to this definition">¶</a></dt> <dd><p>Return <em>x</em> to the power <em>y</em>; if <em>z</em> is present, return <em>x</em> to the power <em>y</em>, modulo <em>z</em> (computed more efficiently than <code class="docutils literal notranslate"><span class="pre">pow(x,</span> <span class="pre">y)</span> <span class="pre">%</span> <span class="pre">z</span></code>). The two-argument form <code class="docutils literal notranslate"><span class="pre">pow(x,</span> <span class="pre">y)</span></code> is equivalent to using the power operator: <code class="docutils literal notranslate"><span class="pre">x**y</span></code>.</p> <p>The arguments must have numeric types. With mixed operand types, the coercion rules for binary arithmetic operators apply. For int and long int operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, <code class="docutils literal notranslate"><span class="pre">10**2</span></code> returns <code class="docutils literal notranslate"><span class="pre">100</span></code>, but <code class="docutils literal notranslate"><span class="pre">10**-2</span></code> returns <code class="docutils literal notranslate"><span class="pre">0.01</span></code>. (This last feature was added in Python 2.2. In Python 2.1 and before, if both arguments were of integer types and the second argument was negative, an exception was raised.) If the second argument is negative, the third argument must be omitted. If <em>z</em> is present, <em>x</em> and <em>y</em> must be of integer types, and <em>y</em> must be non-negative. (This restriction was added in Python 2.2. In Python 2.1 and before, floating 3-argument <code class="docutils literal notranslate"><span class="pre">pow()</span></code> returned platform-dependent results depending on floating-point rounding accidents.)</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="print"> <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">sys.stdout</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#print" title="Permalink to this definition">¶</a></dt> <dd><p>Print <em>objects</em> to the stream <em>file</em>, separated by <em>sep</em> and followed by <em>end</em>. <em>sep</em>, <em>end</em> and <em>file</em>, if present, must be given as keyword arguments.</p> <p>All non-keyword arguments are converted to strings like <a class="reference internal" href="#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> does and written to the stream, separated by <em>sep</em> and followed by <em>end</em>. Both <em>sep</em> 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 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 <em>end</em>.</p> <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 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. Output buffering is determined by <em>file</em>. Use <code class="docutils literal notranslate"><span class="pre">file.flush()</span></code> to ensure, for instance, immediate appearance on a screen.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This function is not normally available as a built-in since the name <code class="docutils literal notranslate"><span class="pre">print</span></code> is recognized as the <a class="reference internal" href="../reference/simple_stmts.html#print"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">print</span></code></a> statement. To disable the statement and use the <a class="reference internal" href="#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> function, use this future statement at the top of your module:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span> </pre></div> </div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.6.</span></p> </div> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="property"> <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><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">fget</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">fset</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">fdel</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">doc</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#property" title="Permalink to this definition">¶</a></dt> <dd><p>Return a property attribute for <a class="reference internal" href="../glossary.html#term-new-style-class"><span class="xref std std-term">new-style class</span></a>es (classes that derive from <a class="reference internal" href="#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a>).</p> <p><em>fget</em> is a function for getting an attribute value. <em>fset</em> is a function for setting an attribute value. <em>fdel</em> is a function for deleting an attribute value. And <em>doc</em> creates a docstring for the attribute.</p> <p>A typical use is to define a managed attribute <code class="docutils literal notranslate"><span class="pre">x</span></code>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <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="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="kc">None</span> <span class="k">def</span> <span class="nf">getx</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <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> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="n">value</span> <span class="k">def</span> <span class="nf">delx</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <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">"I'm the 'x' property."</span><span class="p">)</span> </pre></div> </div> <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, <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> <p>If given, <em>doc</em> will be the docstring of the property attribute. Otherwise, the property will copy <em>fget</em>’s docstring (if it exists). This makes it possible to 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> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Parrot</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <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="bp">self</span><span class="o">.</span><span class="n">_voltage</span> <span class="o">=</span> <span class="mi">100000</span> <span class="nd">@property</span> <span class="k">def</span> <span class="nf">voltage</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="sd">"""Get the current voltage."""</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_voltage</span> </pre></div> </div> <p>The <code class="docutils literal notranslate"><span class="pre">@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” for a read-only attribute with the same name, and it sets the docstring for <em>voltage</em> to “Get the current voltage.”</p> <p>A property object has <code class="xref py py-attr docutils literal notranslate"><span class="pre">getter</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">setter</span></code>, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">deleter</span></code> methods usable as decorators that create a copy of the property with the corresponding accessor function set to the decorated function. This is best explained with an example:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <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="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="kc">None</span> <span class="nd">@property</span> <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="sd">"""I'm the 'x' property."""</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="nd">@x</span><span class="o">.</span><span class="n">setter</span> <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> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="n">value</span> <span class="nd">@x</span><span class="o">.</span><span class="n">deleter</span> <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="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> </pre></div> </div> <p>This code is exactly equivalent to the first example. Be sure to give the additional functions the same name as the original property (<code class="docutils literal notranslate"><span class="pre">x</span></code> in this case.)</p> <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 <code class="docutils literal notranslate"><span class="pre">fdel</span></code> corresponding to the constructor arguments.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.5: </span>Use <em>fget</em>’s docstring if no <em>doc</em> given.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.6: </span>The <code class="docutils literal notranslate"><span class="pre">getter</span></code>, <code class="docutils literal notranslate"><span class="pre">setter</span></code>, and <code class="docutils literal notranslate"><span class="pre">deleter</span></code> attributes were added.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="range"> <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><a class="headerlink" href="#range" title="Permalink to this definition">¶</a></dt> <dt class="sig sig-object py"> <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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">step</span></span></em><span class="optional">]</span><span class="sig-paren">)</span></dt> <dd><p>This is a versatile function to create lists containing arithmetic progressions. It is most often used in <a class="reference internal" href="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> loops. The arguments must be plain integers. If the <em>step</em> argument is omitted, it defaults to <code class="docutils literal notranslate"><span class="pre">1</span></code>. If the <em>start</em> argument is omitted, it defaults to <code class="docutils literal notranslate"><span class="pre">0</span></code>. The full form returns a list of plain integers <code class="docutils literal notranslate"><span class="pre">[start,</span> <span class="pre">start</span> <span class="pre">+</span> <span class="pre">step,</span> <span class="pre">start</span> <span class="pre">+</span> <span class="pre">2</span> <span class="pre">*</span> <span class="pre">step,</span> <span class="pre">...]</span></code>. If <em>step</em> is positive, the last element is the largest <code class="docutils literal notranslate"><span class="pre">start</span> <span class="pre">+</span> <span class="pre">i</span> <span class="pre">*</span> <span class="pre">step</span></code> less than <em>stop</em>; if <em>step</em> is negative, the last element is the smallest <code class="docutils literal notranslate"><span class="pre">start</span> <span class="pre">+</span> <span class="pre">i</span> <span class="pre">*</span> <span class="pre">step</span></code> greater than <em>stop</em>. <em>step</em> must not be zero (or else <code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code> is raised). Example:</p> <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="go">[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</span> <span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">11</span><span class="p">)</span> <span class="go">[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span> <span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="go">[0, 5, 10, 15, 20, 25]</span> <span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="go">[0, 3, 6, 9]</span> <span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">10</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="go">[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]</span> <span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="go">[]</span> <span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="go">[]</span> </pre></div> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="raw_input"> <span class="sig-name descname"><span class="pre">raw_input</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">prompt</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#raw_input" title="Permalink to this definition">¶</a></dt> <dd><p>If the <em>prompt</em> argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, <code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code> is raised. Example:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">raw_input</span><span class="p">(</span><span class="s1">'--> '</span><span class="p">)</span> <span class="go">--> Monty Python's Flying Circus</span> <span class="gp">>>> </span><span class="n">s</span> <span class="go">"Monty Python's Flying Circus"</span> </pre></div> </div> <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="#raw_input" title="raw_input"><code class="xref py py-func docutils literal notranslate"><span class="pre">raw_input()</span></code></a> will use it to provide elaborate line editing and history features.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="reduce"> <span class="sig-name descname"><span class="pre">reduce</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="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">initializer</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#reduce" title="Permalink to this definition">¶</a></dt> <dd><p>Apply <em>function</em> of two arguments cumulatively to the items of <em>iterable</em>, from left to right, so as to reduce the iterable to a single value. For example, <code class="docutils literal notranslate"><span class="pre">reduce(lambda</span> <span class="pre">x,</span> <span class="pre">y:</span> <span class="pre">x+y,</span> <span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3,</span> <span class="pre">4,</span> <span class="pre">5])</span></code> calculates <code class="docutils literal notranslate"><span class="pre">((((1+2)+3)+4)+5)</span></code>. The left argument, <em>x</em>, is the accumulated value and the right argument, <em>y</em>, is the update value from the <em>iterable</em>. If the optional <em>initializer</em> is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If <em>initializer</em> is not given and <em>iterable</em> contains only one item, the first item is returned. Roughly equivalent to:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">reduce</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">iterable</span><span class="p">,</span> <span class="n">initializer</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> <span class="n">it</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="k">if</span> <span class="n">initializer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">try</span><span class="p">:</span> <span class="n">initializer</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">)</span> <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span> <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">'reduce() of empty sequence with no initial value'</span><span class="p">)</span> <span class="n">accum_value</span> <span class="o">=</span> <span class="n">initializer</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span> <span class="n">accum_value</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">accum_value</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span> <span class="k">return</span> <span class="n">accum_value</span> </pre></div> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="reload"> <span class="sig-name descname"><span class="pre">reload</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#reload" title="Permalink to this definition">¶</a></dt> <dd><p>Reload a previously imported <em>module</em>. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter. The return value is the module object (the same as the <em>module</em> argument).</p> <p>When <code class="docutils literal notranslate"><span class="pre">reload(module)</span></code> is executed:</p> <ul class="simple"> <li><p>Python modules’ code is recompiled and the module-level code reexecuted, defining a new set of objects which are bound to names in the module’s dictionary. The <code class="docutils literal notranslate"><span class="pre">init</span></code> function of extension modules is not called a second time.</p></li> <li><p>As with all other objects in Python the old objects are only reclaimed after their reference counts drop to zero.</p></li> <li><p>The names in the module namespace are updated to point to any new or changed objects.</p></li> <li><p>Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired.</p></li> </ul> <p>There are a number of other caveats:</p> <p>When a module is reloaded, its dictionary (containing the module’s global variables) is retained. Redefinitions of names will override the old definitions, so this is generally not a problem. If the new version of a module does not define a name that was defined by the old version, the old definition remains. This feature can be used to the module’s advantage if it maintains a global table or cache of objects — with a <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> statement it can test for the table’s presence and skip its initialization if desired:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span> <span class="n">cache</span> <span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span> <span class="n">cache</span> <span class="o">=</span> <span class="p">{}</span> </pre></div> </div> <p>It is generally not very useful to reload built-in or dynamically loaded modules. Reloading <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a>, <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code> and other key modules is not recommended. In many cases extension modules are not designed to be initialized more than once, and may fail in arbitrary ways when reloaded.</p> <p>If a module imports objects from another module using <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> … <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> …, calling <a class="reference internal" href="#reload" title="reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code></a> for the other module does not redefine the objects imported from it — one way around this is to re-execute the <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> statement, another is to use <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> and qualified names (<em>module</em>.*name*) instead.</p> <p>If a module instantiates instances of a class, reloading the module that defines the class does not affect the method definitions of the instances — they continue to use the old class definition. The same is true for derived classes.</p> </dd></dl> <span class="target" id="func-repr"></span><dl class="py function"> <dt class="sig sig-object py" id="repr"> <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="Permalink to this definition">¶</a></dt> <dd><p>Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. For many types, this function makes an attempt to return a string that would yield an 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 representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code> method.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="reversed"> <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="Permalink to this definition">¶</a></dt> <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 a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__reversed__()</span></code> method or supports the sequence protocol (the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__len__()</span></code> method and the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code> method with integer arguments starting at <code class="docutils literal notranslate"><span class="pre">0</span></code>).</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.4.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.6: </span>Added the possibility to write a custom <code class="xref py py-meth docutils literal notranslate"><span class="pre">__reversed__()</span></code> method.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="round"> <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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">ndigits</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#round" title="Permalink to this definition">¶</a></dt> <dd><p>Return the floating point value <em>number</em> rounded to <em>ndigits</em> digits after the decimal point. If <em>ndigits</em> is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus <em>ndigits</em>; if two multiples are equally close, rounding is done away from 0 (so, for example, <code class="docutils literal notranslate"><span class="pre">round(0.5)</span></code> is <code class="docutils literal notranslate"><span class="pre">1.0</span></code> and <code class="docutils literal notranslate"><span class="pre">round(-0.5)</span></code> is <code class="docutils literal notranslate"><span class="pre">-1.0</span></code>).</p> <div class="admonition note"> <p class="admonition-title">Note</p> <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, <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>. This is not a bug: it’s a result of the fact that most decimal fractions 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 more information.</p> </div> </dd></dl> <span class="target" id="func-set"></span><dl class="py class"> <dt class="sig sig-object py"> <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><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="optional">]</span><span class="sig-paren">)</span></dt> <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 <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 <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> <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>, <code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code>, <a class="reference internal" href="#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: High-performance datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.4.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="setattr"> <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="Permalink to this definition">¶</a></dt> <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 string and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the 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 <code class="docutils literal notranslate"><span class="pre">x.foobar</span> <span class="pre">=</span> <span class="pre">123</span></code>.</p> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="slice"> <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="Permalink to this definition">¶</a></dt> <dt class="sig sig-object py"> <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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">step</span></span></em><span class="optional">]</span><span class="sig-paren">)</span></dt> <dd><p id="index-6">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 <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 <code class="docutils literal notranslate"><span class="pre">None</span></code>. Slice objects have read-only data attributes <code class="xref py py-attr docutils literal notranslate"><span class="pre">start</span></code>, <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 values (or their default). They have no other explicit functionality; however they are used by Numerical Python and other third party extensions. Slice objects are also generated when extended indexing syntax is used. For 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 <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 iterator.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="sorted"> <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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">cmp</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">reverse</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#sorted" title="Permalink to this definition">¶</a></dt> <dd><p>Return a new sorted list from the items in <em>iterable</em>.</p> <p>The optional arguments <em>cmp</em>, <em>key</em>, and <em>reverse</em> have the same meaning as those for the <code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code> method (described in section <a class="reference internal" href="stdtypes.html#typesseq-mutable"><span class="std std-ref">Mutable Sequence Types</span></a>).</p> <p><em>cmp</em> specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: <code class="docutils literal notranslate"><span class="pre">cmp=lambda</span> <span class="pre">x,y:</span> <span class="pre">cmp(x.lower(),</span> <span class="pre">y.lower())</span></code>. The default value is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> <p><em>key</em> specifies a function of one argument that is used to extract a comparison key from each list element: <code class="docutils literal notranslate"><span class="pre">key=str.lower</span></code>. The default value is <code class="docutils literal notranslate"><span class="pre">None</span></code> (compare the elements directly).</p> <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 sorted as if each comparison were reversed.</p> <p>In general, the <em>key</em> and <em>reverse</em> conversion processes are much faster than specifying an equivalent <em>cmp</em> function. This is because <em>cmp</em> is called multiple times for each list element while <em>key</em> and <em>reverse</em> touch each element only once. 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 <em>key</em> function.</p> <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 stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).</p> <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 HOW TO</span></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.4.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="staticmethod"> <span class="sig-name descname"><span class="pre">staticmethod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">function</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#staticmethod" title="Permalink to this definition">¶</a></dt> <dd><p>Return a static method for <em>function</em>.</p> <p>A static method does not receive an implicit first argument. To declare a static method, use this idiom:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="nd">@staticmethod</span> <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="o">...</span><span class="p">):</span> <span class="o">...</span> </pre></div> </div> <p>The <code class="docutils literal notranslate"><span class="pre">@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 <a class="reference internal" href="../reference/compound_stmts.html#function"><span class="std std-ref">Function definitions</span></a> for details.</p> <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 an instance (such as <code class="docutils literal notranslate"><span class="pre">C().f()</span></code>).</p> <p>Static methods in Python are similar to those found in Java or C++. Also see <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 constructors.</p> <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> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.4: </span>Function decorator syntax added.</p> </div> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="str"> <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><a class="headerlink" href="#str" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with <code class="docutils literal notranslate"><span class="pre">repr(object)</span></code> is that <code class="docutils literal notranslate"><span class="pre">str(object)</span></code> does not always attempt to return a string that is acceptable 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>; its goal is to return a printable string. If no argument is given, returns the empty string, <code class="docutils literal notranslate"><span class="pre">''</span></code>.</p> <p>For more information on strings see <a class="reference internal" href="stdtypes.html#typesseq"><span class="std std-ref">Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange</span></a> which describes sequence functionality (strings are sequences), and also the string-specific methods described in the <a class="reference internal" href="stdtypes.html#string-methods"><span class="std std-ref">String Methods</span></a> section. To output formatted strings use template strings or the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator described in the <a class="reference internal" href="stdtypes.html#string-formatting"><span class="std std-ref">String Formatting Operations</span></a> section. In addition see the <a class="reference internal" href="strings.html#stringservices"><span class="std std-ref">String Services</span></a> section. See also <a class="reference internal" href="#unicode" title="unicode"><code class="xref py py-func docutils literal notranslate"><span class="pre">unicode()</span></code></a>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="sum"> <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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">start</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#sum" title="Permalink to this definition">¶</a></dt> <dd><p>Sums <em>start</em> and the items of an <em>iterable</em> from left to right and returns the total. <em>start</em> defaults to <code class="docutils literal notranslate"><span class="pre">0</span></code>. The <em>iterable</em>’s items are normally numbers, and the start value is not allowed to be a string.</p> <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>. The preferred, fast way to concatenate a sequence of strings is by calling <code class="docutils literal notranslate"><span class="pre">''.join(sequence)</span></code>. To add floating point values with extended precision, 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 <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> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.3.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="super"> <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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">object-or-type</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#super" title="Permalink to this definition">¶</a></dt> <dd><p>Return a proxy object that delegates method calls to a parent or sibling class of <em>type</em>. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by <a class="reference internal" href="#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> except that the <em>type</em> itself is skipped.</p> <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>type</em> lists the method 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 attribute is dynamic and can change whenever the inheritance hierarchy is updated.</p> <p>If the second argument is omitted, the super object returned is unbound. If 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 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 is useful for classmethods).</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p><a class="reference internal" href="#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a> only works for <a class="reference internal" href="../glossary.html#term-new-style-class"><span class="xref std std-term">new-style class</span></a>es.</p> </div> <p>There are two typical use cases for <em>super</em>. In a class hierarchy with single inheritance, <em>super</em> can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of <em>super</em> in other programming languages.</p> <p>The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).</p> <p>For both use cases, a typical superclass call looks like this:</p> <div class="highlight-default 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> <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> <span class="nb">super</span><span class="p">(</span><span class="n">C</span><span class="p">,</span> <span class="bp">self</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> </pre></div> </div> <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 explicit dotted attribute lookups such as <code class="docutils literal notranslate"><span class="pre">super().__getitem__(name)</span></code>. It does so by implementing its own <code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code> method for searching classes in a predictable order that supports cooperative multiple inheritance. 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 operators such as <code class="docutils literal notranslate"><span class="pre">super()[name]</span></code>.</p> <p>Also 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 not limited to use inside methods. The two argument form specifies the arguments exactly and makes the appropriate references.</p> <p>For practical suggestions on how to design cooperative classes using <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> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.2.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="tuple"> <span class="sig-name descname"><span class="pre">tuple</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#tuple" title="Permalink to this definition">¶</a></dt> <dd><p>Return a tuple whose items are the same and in the same order as <em>iterable</em>’s items. <em>iterable</em> may be a sequence, a container that supports iteration, or an iterator object. If <em>iterable</em> is already a tuple, it is returned unchanged. For instance, <code class="docutils literal notranslate"><span class="pre">tuple('abc')</span></code> returns <code class="docutils literal notranslate"><span class="pre">('a',</span> <span class="pre">'b',</span> <span class="pre">'c')</span></code> and <code class="docutils literal notranslate"><span class="pre">tuple([1,</span> <span class="pre">2,</span> <span class="pre">3])</span></code> returns <code class="docutils literal notranslate"><span class="pre">(1,</span> <span class="pre">2,</span> <span class="pre">3)</span></code>. If no argument is given, returns a new empty tuple, <code class="docutils literal notranslate"><span class="pre">()</span></code>.</p> <p><a class="reference internal" href="#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> is an immutable sequence type, as documented in <a class="reference internal" href="stdtypes.html#typesseq"><span class="std std-ref">Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange</span></a>. For other containers see the built in <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>, <code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code>, and <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> classes, and the <a class="reference internal" href="collections.html#module-collections" title="collections: High-performance datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module.</p> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="type"> <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="Permalink to this definition">¶</a></dt> <dt class="sig sig-object py"> <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><span class="sig-paren">)</span></dt> <dd><p id="index-7">With one argument, return the type of an <em>object</em>. The return value is a type object. 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 of an object.</p> <p>With three arguments, return a new type object. This is essentially a 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 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; the <em>bases</em> tuple itemizes the base classes and becomes the <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; and the <em>dict</em> dictionary is the namespace containing definitions for class body and becomes 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 example, 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> <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">X</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="gp">... </span> <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">X</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="s1">'X'</span><span class="p">,</span> <span class="p">(</span><span class="nb">object</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> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.2.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="unichr"> <span class="sig-name descname"><span class="pre">unichr</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="#unichr" title="Permalink to this definition">¶</a></dt> <dd><p>Return the Unicode string of one character whose Unicode code is the integer <em>i</em>. For example, <code class="docutils literal notranslate"><span class="pre">unichr(97)</span></code> returns the string <code class="docutils literal notranslate"><span class="pre">u'a'</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> for Unicode strings. The valid range for the argument depends how Python was configured – it may be either UCS2 [0..0xFFFF] or UCS4 [0..0x10FFFF]. <code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code> is raised otherwise. For ASCII and 8-bit strings see <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> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.0.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="unicode"> <span class="sig-name descname"><span class="pre">unicode</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><a class="headerlink" href="#unicode" title="Permalink to this definition">¶</a></dt> <dt class="sig sig-object py"> <span class="sig-name descname"><span class="pre">unicode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt> <dd><p>Return the Unicode string version of <em>object</em> using one of the following modes:</p> <p>If <em>encoding</em> and/or <em>errors</em> are given, <code class="docutils literal notranslate"><span class="pre">unicode()</span></code> will decode the object which can either be an 8-bit string or a character buffer using the codec for <em>encoding</em>. The <em>encoding</em> parameter is a string giving the name of an encoding; if the encoding is not known, <code class="xref py py-exc docutils literal notranslate"><span class="pre">LookupError</span></code> is raised. Error handling is done according to <em>errors</em>; this specifies the treatment of characters which are invalid in the input encoding. If <em>errors</em> is <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> (the default), a <code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code> is raised on errors, while a value of <code class="docutils literal notranslate"><span class="pre">'ignore'</span></code> causes errors to be silently ignored, and a value of <code class="docutils literal notranslate"><span class="pre">'replace'</span></code> causes the official Unicode replacement character, <code class="docutils literal notranslate"><span class="pre">U+FFFD</span></code>, to be used to replace input characters which cannot be decoded. See also 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.</p> <p>If no optional parameters are given, <code class="docutils literal notranslate"><span class="pre">unicode()</span></code> will mimic the behaviour of <code class="docutils literal notranslate"><span class="pre">str()</span></code> except that it returns Unicode strings instead of 8-bit strings. More precisely, if <em>object</em> is a Unicode string or subclass it will return that Unicode string without any additional decoding applied.</p> <p>For objects which provide a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__unicode__()</span></code> method, it will call this method without arguments to create a Unicode string. For all other objects, the 8-bit string version or representation is requested and then converted to a Unicode string using the codec for the default encoding in <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> mode.</p> <p>For more information on Unicode strings see <a class="reference internal" href="stdtypes.html#typesseq"><span class="std std-ref">Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange</span></a> which describes sequence functionality (Unicode strings are sequences), and also the string-specific methods described in the <a class="reference internal" href="stdtypes.html#string-methods"><span class="std std-ref">String Methods</span></a> section. To output formatted strings use template strings or the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator described in the <a class="reference internal" href="stdtypes.html#string-formatting"><span class="std std-ref">String Formatting Operations</span></a> section. In addition see the <a class="reference internal" href="strings.html#stringservices"><span class="std std-ref">String Services</span></a> section. See also <a class="reference internal" href="#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.0.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.2: </span>Support for <code class="xref py py-meth docutils literal notranslate"><span class="pre">__unicode__()</span></code> added.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="vars"> <span class="sig-name descname"><span class="pre">vars</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#vars" title="Permalink to this definition">¶</a></dt> <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, 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> <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> attribute; however, other objects may have write restrictions on their <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, new-style classes use a dictproxy to prevent direct dictionary updates).</p> <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 locals dictionary is only useful for reads since updates to the locals dictionary are ignored.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="xrange"> <span class="sig-name descname"><span class="pre">xrange</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="#xrange" title="Permalink to this definition">¶</a></dt> <dt class="sig sig-object py"> <span class="sig-name descname"><span class="pre">xrange</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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">step</span></span></em><span class="optional">]</span><span class="sig-paren">)</span></dt> <dd><p>This function is very similar to <a class="reference internal" href="#range" title="range"><code class="xref py py-func docutils literal notranslate"><span class="pre">range()</span></code></a>, but returns an <a class="reference internal" href="stdtypes.html#typesseq-xrange"><span class="std std-ref">xrange object</span></a> instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. The advantage of <a class="reference internal" href="#xrange" title="xrange"><code class="xref py py-func docutils literal notranslate"><span class="pre">xrange()</span></code></a> over <a class="reference internal" href="#range" title="range"><code class="xref py py-func docutils literal notranslate"><span class="pre">range()</span></code></a> is minimal (since <a class="reference internal" href="#xrange" title="xrange"><code class="xref py py-func docutils literal notranslate"><span class="pre">xrange()</span></code></a> still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all of the range’s elements are never used (such as when the loop is usually terminated with <a class="reference internal" href="../reference/simple_stmts.html#break"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">break</span></code></a>). For more information on xrange objects, see <a class="reference internal" href="stdtypes.html#typesseq-xrange"><span class="std std-ref">XRange Type</span></a> and <a class="reference internal" href="stdtypes.html#typesseq"><span class="std std-ref">Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange</span></a>.</p> <div class="impl-detail compound"> <p><strong>CPython implementation detail:</strong> <a class="reference internal" href="#xrange" title="xrange"><code class="xref py py-func docutils literal notranslate"><span class="pre">xrange()</span></code></a> is intended to be simple and fast. Implementations may impose restrictions to achieve this. The C implementation of Python restricts all arguments to native C longs (“short” Python integers), and also requires that the number of elements fit in a native C long. If a larger range is needed, an alternate version can be crafted using the <a class="reference internal" href="itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> module: <code class="docutils literal notranslate"><span class="pre">islice(count(start,</span> <span class="pre">step),</span> <span class="pre">(stop-start+step-1+2*(step<0))//step)</span></code>.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="zip"> <span class="sig-name descname"><span class="pre">zip</span></span><span class="sig-paren">(</span><span class="optional">[</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">...</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zip" title="Permalink to this definition">¶</a></dt> <dd><p>This function returns a list of tuples, where the <em>i</em>-th tuple contains the <em>i</em>-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length, <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 similar to <a class="reference internal" href="#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a> with an initial argument of <code class="docutils literal notranslate"><span class="pre">None</span></code>. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.</p> <p>The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using <code class="docutils literal notranslate"><span class="pre">zip(*[iter(s)]*n)</span></code>.</p> <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 list:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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> <span class="gp">>>> </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> <span class="gp">>>> </span><span class="n">zipped</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> <span class="gp">>>> </span><span class="n">zipped</span> <span class="go">[(1, 4), (2, 5), (3, 6)]</span> <span class="gp">>>> </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="n">zipped</span><span class="p">)</span> <span class="gp">>>> </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> <span class="go">True</span> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 2.0.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.4: </span>Formerly, <a class="reference internal" href="#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> required at least one argument and <code class="docutils literal notranslate"><span class="pre">zip()</span></code> raised a <code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code> instead of returning an empty list.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="import__"> <span id="__import__"></span><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><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">globals</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">locals</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">fromlist</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">level</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#import__" title="Permalink to this definition">¶</a></dt> <dd><div class="admonition note" id="index-8"> <p class="admonition-title">Note</p> <p>This is an advanced function that is not needed in everyday Python 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> </div> <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 replaced (by importing the <a class="reference internal" href="__builtin__.html#module-__builtin__" title="__builtin__: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__builtin__</span></code></a> module and assigning to <code class="docutils literal notranslate"><span class="pre">__builtin__.__import__</span></code>) in order to change semantics 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, but nowadays it is usually simpler to use import hooks (see <span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a>). 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 rare, except in cases where you want to import a module whose name is only known at runtime.</p> <p>The function imports the module <em>name</em>, potentially using the given <em>globals</em> and <em>locals</em> to determine how to interpret the name in a package context. The <em>fromlist</em> gives the names of objects or submodules that should be imported from the module given by <em>name</em>. The standard implementation does not use its <em>locals</em> argument at all, and uses its <em>globals</em> only to 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> <p><em>level</em> specifies whether to use absolute or relative imports. The default is <code class="docutils literal notranslate"><span class="pre">-1</span></code> which indicates both absolute and relative imports will be attempted. <code class="docutils literal notranslate"><span class="pre">0</span></code> means only perform absolute imports. Positive values for <em>level</em> indicate the number of parent directories to search relative to the 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>.</p> <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 top-level package (the name up till the first dot) is returned, <em>not</em> the module named by <em>name</em>. However, when a non-empty <em>fromlist</em> argument is given, the module named by <em>name</em> is returned.</p> <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 following code:</p> <div class="highlight-default 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">'spam'</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="o">-</span><span class="mi">1</span><span class="p">)</span> </pre></div> </div> <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> <div class="highlight-default 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">'spam.ham'</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="o">-</span><span class="mi">1</span><span class="p">)</span> </pre></div> </div> <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 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> <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> <span class="pre">saus</span></code> results in</p> <div class="highlight-default 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">'spam.ham'</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">'eggs'</span><span class="p">,</span> <span class="s1">'sausage'</span><span class="p">],</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="n">eggs</span> <span class="o">=</span> <span class="n">_temp</span><span class="o">.</span><span class="n">eggs</span> <span class="n">saus</span> <span class="o">=</span> <span class="n">_temp</span><span class="o">.</span><span class="n">sausage</span> </pre></div> </div> <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 object, the names to import are retrieved and assigned to their respective names.</p> <p>If you simply want to import a module (potentially within a package) by name, 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> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.5: </span>The level parameter was added.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.5: </span>Keyword support for parameters was added.</p> </div> </dd></dl> </section> <section id="non-essential-built-in-functions"> <span id="non-essential-built-in-funcs"></span><h1><span class="section-number">3. </span>Non-essential Built-in Functions<a class="headerlink" href="#non-essential-built-in-functions" title="Permalink to this headline">¶</a></h1> <p>There are several built-in functions that are no longer essential to learn, know or use in modern Python programming. They have been kept here to maintain backwards compatibility with programs written for older versions of Python.</p> <p>Python programmers, trainers, students and book writers should feel free to bypass these functions without concerns about missing something important.</p> <dl class="py function"> <dt class="sig sig-object py" id="apply"> <span class="sig-name descname"><span class="pre">apply</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">args</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">keywords</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#apply" title="Permalink to this definition">¶</a></dt> <dd><p>The <em>function</em> argument must be a callable object (a user-defined or built-in function or method, or a class object) and the <em>args</em> argument must be a sequence. The <em>function</em> is called with <em>args</em> as the argument list; the number of arguments is the length of the tuple. If the optional <em>keywords</em> argument is present, it must be a dictionary whose keys are strings. It specifies keyword arguments to be added to the end of the argument list. Calling <a class="reference internal" href="#apply" title="apply"><code class="xref py py-func docutils literal notranslate"><span class="pre">apply()</span></code></a> is different from just calling <code class="docutils literal notranslate"><span class="pre">function(args)</span></code>, since in that case there is always exactly one argument. The use of <a class="reference internal" href="#apply" title="apply"><code class="xref py py-func docutils literal notranslate"><span class="pre">apply()</span></code></a> is equivalent to <code class="docutils literal notranslate"><span class="pre">function(*args,</span> <span class="pre">**keywords)</span></code>.</p> <div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 2.3: </span>Use <code class="docutils literal notranslate"><span class="pre">function(*args,</span> <span class="pre">**keywords)</span></code> instead of <code class="docutils literal notranslate"><span class="pre">apply(function,</span> <span class="pre">args,</span> <span class="pre">keywords)</span></code> (see <a class="reference internal" href="../tutorial/controlflow.html#tut-unpacking-arguments"><span class="std std-ref">Unpacking Argument Lists</span></a>).</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="buffer"> <span class="sig-name descname"><span class="pre">buffer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">size</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#buffer" title="Permalink to this definition">¶</a></dt> <dd><p>The <em>object</em> argument must be an object that supports the buffer call interface (such as strings, arrays, and buffers). A new buffer object will be created which references the <em>object</em> argument. The buffer object will be a slice from the beginning of <em>object</em> (or from the specified <em>offset</em>). The slice will extend to the end of <em>object</em> (or will have a length given by the <em>size</em> argument).</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="coerce"> <span class="sig-name descname"><span class="pre">coerce</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">y</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#coerce" title="Permalink to this definition">¶</a></dt> <dd><p>Return a tuple consisting of the two numeric arguments converted to a common type, using the same rules as used by arithmetic operations. If coercion is not possible, raise <code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="intern"> <span class="sig-name descname"><span class="pre">intern</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><a class="headerlink" href="#intern" title="Permalink to this definition">¶</a></dt> <dd><p>Enter <em>string</em> in the table of “interned” strings and return the interned string – which is <em>string</em> itself or a copy. Interning strings is useful to gain a little performance on dictionary lookup – if the keys in a dictionary are interned, and the lookup key is interned, the key comparisons (after hashing) can be done by a pointer compare instead of a string compare. Normally, the names used in Python programs are automatically interned, and the dictionaries used to hold module, class or instance attributes have interned keys.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 2.3: </span>Interned strings are not immortal (like they used to be in Python 2.2 and before); you must keep a reference to the return value of <a class="reference internal" href="#intern" title="intern"><code class="xref py py-func docutils literal notranslate"><span class="pre">intern()</span></code></a> around to benefit from it.</p> </div> </dd></dl> <p class="rubric">Footnotes</p> <dl class="footnote brackets"> <dt class="label" id="id3"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt> <dd><p>It is used relatively rarely so does not warrant being made into a statement.</p> </dd> <dt class="label" id="id4"><span class="brackets"><a class="fn-backref" href="#id2">2</a></span></dt> <dd><p>Specifying a buffer size currently has no effect on systems that don’t have <code class="xref c c-func docutils literal notranslate"><span class="pre">setvbuf()</span></code>. The interface to specify the buffer size is not done using a method that calls <code class="xref c c-func docutils literal notranslate"><span class="pre">setvbuf()</span></code>, because that may dump core when called after any I/O has been performed, and there’s no reliable way to determine whether this is the case.</p> </dd> <dt class="label" id="id5"><span class="brackets">3</span></dt> <dd><p>In the current implementation, local variable bindings cannot normally be affected this way, but variables retrieved from other scopes (such as modules) can be. This may change.</p> </dd> </dl> </section> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#">2. Built-in Functions</a></li> <li><a class="reference internal" href="#non-essential-built-in-functions">3. Non-essential Built-in Functions</a></li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="intro.html" title="previous chapter"><span class="section-number">1. </span>Introduction</a></p> <h4>Next topic</h4> <p class="topless"><a href="constants.html" title="next chapter"><span class="section-number">4. </span>Built-in Constants</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/library/functions.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3 id="searchlabel">Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/> <input type="submit" value="Go" /> </form> </div> </div> <script>$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="constants.html" title="4. Built-in Constants" >next</a> |</li> <li class="right" > <a href="intro.html" title="1. Introduction" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.18 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-this"><a href=""><span class="section-number">2. </span>Built-in Functions</a></li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2024, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on November 21, 2024. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 4.3.2. </div> </body> </html>