1967 lines
205 KiB
HTML
1967 lines
205 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<title>Programming FAQ — Python 3.7.4 documentation</title>
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script type="text/javascript" src="../_static/jquery.js"></script>
|
||
<script type="text/javascript" src="../_static/underscore.js"></script>
|
||
<script type="text/javascript" src="../_static/doctools.js"></script>
|
||
<script type="text/javascript" src="../_static/language_data.js"></script>
|
||
|
||
<script type="text/javascript" src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="Search within Python 3.7.4 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="Design and History FAQ" href="design.html" />
|
||
<link rel="prev" title="General Python FAQ" href="general.html" />
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||
<link rel="canonical" href="https://docs.python.org/3/faq/programming.html" />
|
||
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/switchers.js"></script>
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
|
||
|
||
</head><body>
|
||
|
||
<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="design.html" title="Design and History FAQ"
|
||
accesskey="N">next</a> |</li>
|
||
<li class="right" >
|
||
<a href="general.html" title="General Python FAQ"
|
||
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>
|
||
<span class="language_switcher_placeholder">en</span>
|
||
<span class="version_switcher_placeholder">3.7.4</span>
|
||
<a href="../index.html">Documentation </a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python Frequently Asked Questions</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" style="display: none" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" type="text" name="q" />
|
||
<input type="submit" value="Go" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
<script type="text/javascript">$('.inline-search').show(0);</script>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<div class="section" id="programming-faq">
|
||
<h1><a class="toc-backref" href="#id1">Programming FAQ</a><a class="headerlink" href="#programming-faq" title="Permalink to this headline">¶</a></h1>
|
||
<div class="contents topic" id="contents">
|
||
<p class="topic-title first">Contents</p>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="#programming-faq" id="id1">Programming FAQ</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#general-questions" id="id2">General Questions</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#is-there-a-source-code-level-debugger-with-breakpoints-single-stepping-etc" id="id3">Is there a source code level debugger with breakpoints, single-stepping, etc.?</a></p></li>
|
||
<li><p><a class="reference internal" href="#is-there-a-tool-to-help-find-bugs-or-perform-static-analysis" id="id4">Is there a tool to help find bugs or perform static analysis?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-can-i-create-a-stand-alone-binary-from-a-python-script" id="id5">How can I create a stand-alone binary from a Python script?</a></p></li>
|
||
<li><p><a class="reference internal" href="#are-there-coding-standards-or-a-style-guide-for-python-programs" id="id6">Are there coding standards or a style guide for Python programs?</a></p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p><a class="reference internal" href="#core-language" id="id7">Core Language</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value" id="id8">Why am I getting an UnboundLocalError when the variable has a value?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-are-the-rules-for-local-and-global-variables-in-python" id="id9">What are the rules for local and global variables in Python?</a></p></li>
|
||
<li><p><a class="reference internal" href="#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result" id="id10">Why do lambdas defined in a loop with different values all return the same result?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-share-global-variables-across-modules" id="id11">How do I share global variables across modules?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-are-the-best-practices-for-using-import-in-a-module" id="id12">What are the “best practices” for using import in a module?</a></p></li>
|
||
<li><p><a class="reference internal" href="#why-are-default-values-shared-between-objects" id="id13">Why are default values shared between objects?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-can-i-pass-optional-or-keyword-parameters-from-one-function-to-another" id="id14">How can I pass optional or keyword parameters from one function to another?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-is-the-difference-between-arguments-and-parameters" id="id15">What is the difference between arguments and parameters?</a></p></li>
|
||
<li><p><a class="reference internal" href="#why-did-changing-list-y-also-change-list-x" id="id16">Why did changing list ‘y’ also change list ‘x’?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-write-a-function-with-output-parameters-call-by-reference" id="id17">How do I write a function with output parameters (call by reference)?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-you-make-a-higher-order-function-in-python" id="id18">How do you make a higher order function in Python?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-copy-an-object-in-python" id="id19">How do I copy an object in Python?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-can-i-find-the-methods-or-attributes-of-an-object" id="id20">How can I find the methods or attributes of an object?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-can-my-code-discover-the-name-of-an-object" id="id21">How can my code discover the name of an object?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-s-up-with-the-comma-operator-s-precedence" id="id22">What’s up with the comma operator’s precedence?</a></p></li>
|
||
<li><p><a class="reference internal" href="#is-there-an-equivalent-of-c-s-ternary-operator" id="id23">Is there an equivalent of C’s “?:” ternary operator?</a></p></li>
|
||
<li><p><a class="reference internal" href="#is-it-possible-to-write-obfuscated-one-liners-in-python" id="id24">Is it possible to write obfuscated one-liners in Python?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-does-the-slash-in-the-parameter-list-of-a-function-mean" id="id25">What does the slash(/) in the parameter list of a function mean?</a></p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p><a class="reference internal" href="#numbers-and-strings" id="id26">Numbers and strings</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#how-do-i-specify-hexadecimal-and-octal-integers" id="id27">How do I specify hexadecimal and octal integers?</a></p></li>
|
||
<li><p><a class="reference internal" href="#why-does-22-10-return-3" id="id28">Why does -22 // 10 return -3?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-convert-a-string-to-a-number" id="id29">How do I convert a string to a number?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-convert-a-number-to-a-string" id="id30">How do I convert a number to a string?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-modify-a-string-in-place" id="id31">How do I modify a string in place?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-use-strings-to-call-functions-methods" id="id32">How do I use strings to call functions/methods?</a></p></li>
|
||
<li><p><a class="reference internal" href="#is-there-an-equivalent-to-perl-s-chomp-for-removing-trailing-newlines-from-strings" id="id33">Is there an equivalent to Perl’s chomp() for removing trailing newlines from strings?</a></p></li>
|
||
<li><p><a class="reference internal" href="#is-there-a-scanf-or-sscanf-equivalent" id="id34">Is there a scanf() or sscanf() equivalent?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-does-unicodedecodeerror-or-unicodeencodeerror-error-mean" id="id35">What does ‘UnicodeDecodeError’ or ‘UnicodeEncodeError’ error mean?</a></p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p><a class="reference internal" href="#performance" id="id36">Performance</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#my-program-is-too-slow-how-do-i-speed-it-up" id="id37">My program is too slow. How do I speed it up?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-is-the-most-efficient-way-to-concatenate-many-strings-together" id="id38">What is the most efficient way to concatenate many strings together?</a></p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p><a class="reference internal" href="#sequences-tuples-lists" id="id39">Sequences (Tuples/Lists)</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#how-do-i-convert-between-tuples-and-lists" id="id40">How do I convert between tuples and lists?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-s-a-negative-index" id="id41">What’s a negative index?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-iterate-over-a-sequence-in-reverse-order" id="id42">How do I iterate over a sequence in reverse order?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-you-remove-duplicates-from-a-list" id="id43">How do you remove duplicates from a list?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-you-make-an-array-in-python" id="id44">How do you make an array in Python?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-create-a-multidimensional-list" id="id45">How do I create a multidimensional list?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-apply-a-method-to-a-sequence-of-objects" id="id46">How do I apply a method to a sequence of objects?</a></p></li>
|
||
<li><p><a class="reference internal" href="#why-does-a-tuple-i-item-raise-an-exception-when-the-addition-works" id="id47">Why does a_tuple[i] += [‘item’] raise an exception when the addition works?</a></p></li>
|
||
<li><p><a class="reference internal" href="#i-want-to-do-a-complicated-sort-can-you-do-a-schwartzian-transform-in-python" id="id48">I want to do a complicated sort: can you do a Schwartzian Transform in Python?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-can-i-sort-one-list-by-values-from-another-list" id="id49">How can I sort one list by values from another list?</a></p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p><a class="reference internal" href="#objects" id="id50">Objects</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#what-is-a-class" id="id51">What is a class?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-is-a-method" id="id52">What is a method?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-is-self" id="id53">What is self?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-check-if-an-object-is-an-instance-of-a-given-class-or-of-a-subclass-of-it" id="id54">How do I check if an object is an instance of a given class or of a subclass of it?</a></p></li>
|
||
<li><p><a class="reference internal" href="#what-is-delegation" id="id55">What is delegation?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-call-a-method-defined-in-a-base-class-from-a-derived-class-that-overrides-it" id="id56">How do I call a method defined in a base class from a derived class that overrides it?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-can-i-organize-my-code-to-make-it-easier-to-change-the-base-class" id="id57">How can I organize my code to make it easier to change the base class?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-create-static-class-data-and-static-class-methods" id="id58">How do I create static class data and static class methods?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-can-i-overload-constructors-or-methods-in-python" id="id59">How can I overload constructors (or methods) in Python?</a></p></li>
|
||
<li><p><a class="reference internal" href="#i-try-to-use-spam-and-i-get-an-error-about-someclassname-spam" id="id60">I try to use __spam and I get an error about _SomeClassName__spam.</a></p></li>
|
||
<li><p><a class="reference internal" href="#my-class-defines-del-but-it-is-not-called-when-i-delete-the-object" id="id61">My class defines __del__ but it is not called when I delete the object.</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-get-a-list-of-all-instances-of-a-given-class" id="id62">How do I get a list of all instances of a given class?</a></p></li>
|
||
<li><p><a class="reference internal" href="#why-does-the-result-of-id-appear-to-be-not-unique" id="id63">Why does the result of <code class="docutils literal notranslate"><span class="pre">id()</span></code> appear to be not unique?</a></p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p><a class="reference internal" href="#modules" id="id64">Modules</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#how-do-i-create-a-pyc-file" id="id65">How do I create a .pyc file?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-do-i-find-the-current-module-name" id="id66">How do I find the current module name?</a></p></li>
|
||
<li><p><a class="reference internal" href="#how-can-i-have-modules-that-mutually-import-each-other" id="id67">How can I have modules that mutually import each other?</a></p></li>
|
||
<li><p><a class="reference internal" href="#import-x-y-z-returns-module-x-how-do-i-get-z" id="id68">__import__(‘x.y.z’) returns <module ‘x’>; how do I get z?</a></p></li>
|
||
<li><p><a class="reference internal" href="#when-i-edit-an-imported-module-and-reimport-it-the-changes-don-t-show-up-why-does-this-happen" id="id69">When I edit an imported module and reimport it, the changes don’t show up. Why does this happen?</a></p></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="general-questions">
|
||
<h2><a class="toc-backref" href="#id2">General Questions</a><a class="headerlink" href="#general-questions" title="Permalink to this headline">¶</a></h2>
|
||
<div class="section" id="is-there-a-source-code-level-debugger-with-breakpoints-single-stepping-etc">
|
||
<h3><a class="toc-backref" href="#id3">Is there a source code level debugger with breakpoints, single-stepping, etc.?</a><a class="headerlink" href="#is-there-a-source-code-level-debugger-with-breakpoints-single-stepping-etc" title="Permalink to this headline">¶</a></h3>
|
||
<p>Yes.</p>
|
||
<p>Several debuggers for Python are described below, and the built-in function
|
||
<a class="reference internal" href="../library/functions.html#breakpoint" title="breakpoint"><code class="xref py py-func docutils literal notranslate"><span class="pre">breakpoint()</span></code></a> allows you to drop into any of them.</p>
|
||
<p>The pdb module is a simple but adequate console-mode debugger for Python. It is
|
||
part of the standard Python library, and is <a class="reference internal" href="../library/pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">documented</span> <span class="pre">in</span> <span class="pre">the</span> <span class="pre">Library</span>
|
||
<span class="pre">Reference</span> <span class="pre">Manual</span></code></a>. You can also write your own debugger by using the code
|
||
for pdb as an example.</p>
|
||
<p>The IDLE interactive development environment, which is part of the standard
|
||
Python distribution (normally available as Tools/scripts/idle), includes a
|
||
graphical debugger.</p>
|
||
<p>PythonWin is a Python IDE that includes a GUI debugger based on pdb. The
|
||
Pythonwin debugger colors breakpoints and has quite a few cool features such as
|
||
debugging non-Pythonwin programs. Pythonwin is available as part of the <a class="reference external" href="https://sourceforge.net/projects/pywin32/">Python
|
||
for Windows Extensions</a> project and
|
||
as a part of the ActivePython distribution (see
|
||
<a class="reference external" href="https://www.activestate.com/activepython">https://www.activestate.com/activepython</a>).</p>
|
||
<p><a class="reference external" href="http://boa-constructor.sourceforge.net/">Boa Constructor</a> is an IDE and GUI
|
||
builder that uses wxWidgets. It offers visual frame creation and manipulation,
|
||
an object inspector, many views on the source like object browsers, inheritance
|
||
hierarchies, doc string generated html documentation, an advanced debugger,
|
||
integrated help, and Zope support.</p>
|
||
<p><a class="reference external" href="http://eric-ide.python-projects.org/">Eric</a> is an IDE built on PyQt
|
||
and the Scintilla editing component.</p>
|
||
<p>Pydb is a version of the standard Python debugger pdb, modified for use with DDD
|
||
(Data Display Debugger), a popular graphical debugger front end. Pydb can be
|
||
found at <a class="reference external" href="http://bashdb.sourceforge.net/pydb/">http://bashdb.sourceforge.net/pydb/</a> and DDD can be found at
|
||
<a class="reference external" href="https://www.gnu.org/software/ddd">https://www.gnu.org/software/ddd</a>.</p>
|
||
<p>There are a number of commercial Python IDEs that include graphical debuggers.
|
||
They include:</p>
|
||
<ul class="simple">
|
||
<li><p>Wing IDE (<a class="reference external" href="https://wingware.com/">https://wingware.com/</a>)</p></li>
|
||
<li><p>Komodo IDE (<a class="reference external" href="https://komodoide.com/">https://komodoide.com/</a>)</p></li>
|
||
<li><p>PyCharm (<a class="reference external" href="https://www.jetbrains.com/pycharm/">https://www.jetbrains.com/pycharm/</a>)</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="is-there-a-tool-to-help-find-bugs-or-perform-static-analysis">
|
||
<h3><a class="toc-backref" href="#id4">Is there a tool to help find bugs or perform static analysis?</a><a class="headerlink" href="#is-there-a-tool-to-help-find-bugs-or-perform-static-analysis" title="Permalink to this headline">¶</a></h3>
|
||
<p>Yes.</p>
|
||
<p>PyChecker is a static analysis tool that finds bugs in Python source code and
|
||
warns about code complexity and style. You can get PyChecker from
|
||
<a class="reference external" href="http://pychecker.sourceforge.net/">http://pychecker.sourceforge.net/</a>.</p>
|
||
<p><a class="reference external" href="https://www.pylint.org/">Pylint</a> is another tool that checks
|
||
if a module satisfies a coding standard, and also makes it possible to write
|
||
plug-ins to add a custom feature. In addition to the bug checking that
|
||
PyChecker performs, Pylint offers some additional features such as checking line
|
||
length, whether variable names are well-formed according to your coding
|
||
standard, whether declared interfaces are fully implemented, and more.
|
||
<a class="reference external" href="https://docs.pylint.org/">https://docs.pylint.org/</a> provides a full list of Pylint’s features.</p>
|
||
<p>Static type checkers such as <a class="reference external" href="http://mypy-lang.org/">Mypy</a>,
|
||
<a class="reference external" href="https://pyre-check.org/">Pyre</a>, and
|
||
<a class="reference external" href="https://github.com/google/pytype">Pytype</a> can check type hints in Python
|
||
source code.</p>
|
||
</div>
|
||
<div class="section" id="how-can-i-create-a-stand-alone-binary-from-a-python-script">
|
||
<h3><a class="toc-backref" href="#id5">How can I create a stand-alone binary from a Python script?</a><a class="headerlink" href="#how-can-i-create-a-stand-alone-binary-from-a-python-script" title="Permalink to this headline">¶</a></h3>
|
||
<p>You don’t need the ability to compile Python to C code if all you want is a
|
||
stand-alone program that users can download and run without having to install
|
||
the Python distribution first. There are a number of tools that determine the
|
||
set of modules required by a program and bind these modules together with a
|
||
Python binary to produce a single executable.</p>
|
||
<p>One is to use the freeze tool, which is included in the Python source tree as
|
||
<code class="docutils literal notranslate"><span class="pre">Tools/freeze</span></code>. It converts Python byte code to C arrays; a C compiler you can
|
||
embed all your modules into a new program, which is then linked with the
|
||
standard Python modules.</p>
|
||
<p>It works by scanning your source recursively for import statements (in both
|
||
forms) and looking for the modules in the standard Python path as well as in the
|
||
source directory (for built-in modules). It then turns the bytecode for modules
|
||
written in Python into C code (array initializers that can be turned into code
|
||
objects using the marshal module) and creates a custom-made config file that
|
||
only contains those built-in modules which are actually used in the program. It
|
||
then compiles the generated C code and links it with the rest of the Python
|
||
interpreter to form a self-contained binary which acts exactly like your script.</p>
|
||
<p>Obviously, freeze requires a C compiler. There are several other utilities
|
||
which don’t. One is Thomas Heller’s py2exe (Windows only) at</p>
|
||
<blockquote>
|
||
<div><p><a class="reference external" href="http://www.py2exe.org/">http://www.py2exe.org/</a></p>
|
||
</div></blockquote>
|
||
<p>Another tool is Anthony Tuininga’s <a class="reference external" href="https://anthony-tuininga.github.io/cx_Freeze/">cx_Freeze</a>.</p>
|
||
</div>
|
||
<div class="section" id="are-there-coding-standards-or-a-style-guide-for-python-programs">
|
||
<h3><a class="toc-backref" href="#id6">Are there coding standards or a style guide for Python programs?</a><a class="headerlink" href="#are-there-coding-standards-or-a-style-guide-for-python-programs" title="Permalink to this headline">¶</a></h3>
|
||
<p>Yes. The coding style required for standard library modules is documented as
|
||
<span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0008"><strong>PEP 8</strong></a>.</p>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="core-language">
|
||
<h2><a class="toc-backref" href="#id7">Core Language</a><a class="headerlink" href="#core-language" title="Permalink to this headline">¶</a></h2>
|
||
<div class="section" id="why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value">
|
||
<h3><a class="toc-backref" href="#id8">Why am I getting an UnboundLocalError when the variable has a value?</a><a class="headerlink" href="#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value" title="Permalink to this headline">¶</a></h3>
|
||
<p>It can be a surprise to get the UnboundLocalError in previously working
|
||
code when it is modified by adding an assignment statement somewhere in
|
||
the body of a function.</p>
|
||
<p>This code:</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="mi">10</span>
|
||
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">bar</span><span class="p">():</span>
|
||
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">bar</span><span class="p">()</span>
|
||
<span class="go">10</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>works, but this code:</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="mi">10</span>
|
||
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
|
||
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
|
||
<span class="gp">... </span> <span class="n">x</span> <span class="o">+=</span> <span class="mi">1</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>results in an UnboundLocalError:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">foo</span><span class="p">()</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
<span class="c">...</span>
|
||
<span class="gr">UnboundLocalError</span>: <span class="n">local variable 'x' referenced before assignment</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This is because when you make an assignment to a variable in a scope, that
|
||
variable becomes local to that scope and shadows any similarly named variable
|
||
in the outer scope. Since the last statement in foo assigns a new value to
|
||
<code class="docutils literal notranslate"><span class="pre">x</span></code>, the compiler recognizes it as a local variable. Consequently when the
|
||
earlier <code class="docutils literal notranslate"><span class="pre">print(x)</span></code> attempts to print the uninitialized local variable and
|
||
an error results.</p>
|
||
<p>In the example above you can access the outer scope variable by declaring it
|
||
global:</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="mi">10</span>
|
||
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">foobar</span><span class="p">():</span>
|
||
<span class="gp">... </span> <span class="k">global</span> <span class="n">x</span>
|
||
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</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="n">foobar</span><span class="p">()</span>
|
||
<span class="go">10</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This explicit declaration is required in order to remind you that (unlike the
|
||
superficially analogous situation with class and instance variables) you are
|
||
actually modifying the value of the variable in the outer scope:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
|
||
<span class="go">11</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You can do a similar thing in a nested scope using the <a class="reference internal" href="../reference/simple_stmts.html#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a>
|
||
keyword:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
|
||
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="mi">10</span>
|
||
<span class="gp">... </span> <span class="k">def</span> <span class="nf">bar</span><span class="p">():</span>
|
||
<span class="gp">... </span> <span class="k">nonlocal</span> <span class="n">x</span>
|
||
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</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="n">bar</span><span class="p">()</span>
|
||
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">foo</span><span class="p">()</span>
|
||
<span class="go">10</span>
|
||
<span class="go">11</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="what-are-the-rules-for-local-and-global-variables-in-python">
|
||
<h3><a class="toc-backref" href="#id9">What are the rules for local and global variables in Python?</a><a class="headerlink" href="#what-are-the-rules-for-local-and-global-variables-in-python" title="Permalink to this headline">¶</a></h3>
|
||
<p>In Python, variables that are only referenced inside a function are implicitly
|
||
global. If a variable is assigned a value anywhere within the function’s body,
|
||
it’s assumed to be a local unless explicitly declared as global.</p>
|
||
<p>Though a bit surprising at first, a moment’s consideration explains this. On
|
||
one hand, requiring <a class="reference internal" href="../reference/simple_stmts.html#global"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code></a> for assigned variables provides a bar
|
||
against unintended side-effects. On the other hand, if <code class="docutils literal notranslate"><span class="pre">global</span></code> was required
|
||
for all global references, you’d be using <code class="docutils literal notranslate"><span class="pre">global</span></code> all the time. You’d have
|
||
to declare as global every reference to a built-in function or to a component of
|
||
an imported module. This clutter would defeat the usefulness of the <code class="docutils literal notranslate"><span class="pre">global</span></code>
|
||
declaration for identifying side-effects.</p>
|
||
</div>
|
||
<div class="section" id="why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result">
|
||
<h3><a class="toc-backref" href="#id10">Why do lambdas defined in a loop with different values all return the same result?</a><a class="headerlink" href="#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result" title="Permalink to this headline">¶</a></h3>
|
||
<p>Assume you use a for loop to define a few different lambdas (or even plain
|
||
functions), e.g.:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">squares</span> <span class="o">=</span> <span class="p">[]</span>
|
||
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="n">squares</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="k">lambda</span><span class="p">:</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This gives you a list that contains 5 lambdas that calculate <code class="docutils literal notranslate"><span class="pre">x**2</span></code>. You
|
||
might expect that, when called, they would return, respectively, <code class="docutils literal notranslate"><span class="pre">0</span></code>, <code class="docutils literal notranslate"><span class="pre">1</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">4</span></code>, <code class="docutils literal notranslate"><span class="pre">9</span></code>, and <code class="docutils literal notranslate"><span class="pre">16</span></code>. However, when you actually try you will see that
|
||
they all return <code class="docutils literal notranslate"><span class="pre">16</span></code>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">squares</span><span class="p">[</span><span class="mi">2</span><span class="p">]()</span>
|
||
<span class="go">16</span>
|
||
<span class="gp">>>> </span><span class="n">squares</span><span class="p">[</span><span class="mi">4</span><span class="p">]()</span>
|
||
<span class="go">16</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This happens because <code class="docutils literal notranslate"><span class="pre">x</span></code> is not local to the lambdas, but is defined in
|
||
the outer scope, and it is accessed when the lambda is called — not when it
|
||
is defined. At the end of the loop, the value of <code class="docutils literal notranslate"><span class="pre">x</span></code> is <code class="docutils literal notranslate"><span class="pre">4</span></code>, so all the
|
||
functions now return <code class="docutils literal notranslate"><span class="pre">4**2</span></code>, i.e. <code class="docutils literal notranslate"><span class="pre">16</span></code>. You can also verify this by
|
||
changing the value of <code class="docutils literal notranslate"><span class="pre">x</span></code> and see how the results of the lambdas change:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="mi">8</span>
|
||
<span class="gp">>>> </span><span class="n">squares</span><span class="p">[</span><span class="mi">2</span><span class="p">]()</span>
|
||
<span class="go">64</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>In order to avoid this, you need to save the values in variables local to the
|
||
lambdas, so that they don’t rely on the value of the global <code class="docutils literal notranslate"><span class="pre">x</span></code>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">squares</span> <span class="o">=</span> <span class="p">[]</span>
|
||
<span class="gp">>>> </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="n">squares</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="k">lambda</span> <span class="n">n</span><span class="o">=</span><span class="n">x</span><span class="p">:</span> <span class="n">n</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Here, <code class="docutils literal notranslate"><span class="pre">n=x</span></code> creates a new variable <code class="docutils literal notranslate"><span class="pre">n</span></code> local to the lambda and computed
|
||
when the lambda is defined so that it has the same value that <code class="docutils literal notranslate"><span class="pre">x</span></code> had at
|
||
that point in the loop. This means that the value of <code class="docutils literal notranslate"><span class="pre">n</span></code> will be <code class="docutils literal notranslate"><span class="pre">0</span></code>
|
||
in the first lambda, <code class="docutils literal notranslate"><span class="pre">1</span></code> in the second, <code class="docutils literal notranslate"><span class="pre">2</span></code> in the third, and so on.
|
||
Therefore each lambda will now return the correct result:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">squares</span><span class="p">[</span><span class="mi">2</span><span class="p">]()</span>
|
||
<span class="go">4</span>
|
||
<span class="gp">>>> </span><span class="n">squares</span><span class="p">[</span><span class="mi">4</span><span class="p">]()</span>
|
||
<span class="go">16</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Note that this behaviour is not peculiar to lambdas, but applies to regular
|
||
functions too.</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-share-global-variables-across-modules">
|
||
<h3><a class="toc-backref" href="#id11">How do I share global variables across modules?</a><a class="headerlink" href="#how-do-i-share-global-variables-across-modules" title="Permalink to this headline">¶</a></h3>
|
||
<p>The canonical way to share information across modules within a single program is
|
||
to create a special module (often called config or cfg). Just import the config
|
||
module in all modules of your application; the module then becomes available as
|
||
a global name. Because there is only one instance of each module, any changes
|
||
made to the module object get reflected everywhere. For example:</p>
|
||
<p>config.py:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># Default value of the 'x' configuration setting</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>mod.py:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">config</span>
|
||
<span class="n">config</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>main.py:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">config</span>
|
||
<span class="kn">import</span> <span class="nn">mod</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">x</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Note that using a module is also the basis for implementing the Singleton design
|
||
pattern, for the same reason.</p>
|
||
</div>
|
||
<div class="section" id="what-are-the-best-practices-for-using-import-in-a-module">
|
||
<h3><a class="toc-backref" href="#id12">What are the “best practices” for using import in a module?</a><a class="headerlink" href="#what-are-the-best-practices-for-using-import-in-a-module" title="Permalink to this headline">¶</a></h3>
|
||
<p>In general, don’t use <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">modulename</span> <span class="pre">import</span> <span class="pre">*</span></code>. Doing so clutters the
|
||
importer’s namespace, and makes it much harder for linters to detect undefined
|
||
names.</p>
|
||
<p>Import modules at the top of a file. Doing so makes it clear what other modules
|
||
your code requires and avoids questions of whether the module name is in scope.
|
||
Using one import per line makes it easy to add and delete module imports, but
|
||
using multiple imports per line uses less screen space.</p>
|
||
<p>It’s good practice if you import modules in the following order:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>standard library modules – e.g. <code class="docutils literal notranslate"><span class="pre">sys</span></code>, <code class="docutils literal notranslate"><span class="pre">os</span></code>, <code class="docutils literal notranslate"><span class="pre">getopt</span></code>, <code class="docutils literal notranslate"><span class="pre">re</span></code></p></li>
|
||
<li><p>third-party library modules (anything installed in Python’s site-packages
|
||
directory) – e.g. mx.DateTime, ZODB, PIL.Image, etc.</p></li>
|
||
<li><p>locally-developed modules</p></li>
|
||
</ol>
|
||
<p>It is sometimes necessary to move imports to a function or class to avoid
|
||
problems with circular imports. Gordon McMillan says:</p>
|
||
<blockquote>
|
||
<div><p>Circular imports are fine where both modules use the “import <module>” form
|
||
of import. They fail when the 2nd module wants to grab a name out of the
|
||
first (“from module import name”) and the import is at the top level. That’s
|
||
because names in the 1st are not yet available, because the first module is
|
||
busy importing the 2nd.</p>
|
||
</div></blockquote>
|
||
<p>In this case, if the second module is only used in one function, then the import
|
||
can easily be moved into that function. By the time the import is called, the
|
||
first module will have finished initializing, and the second module can do its
|
||
import.</p>
|
||
<p>It may also be necessary to move imports out of the top level of code if some of
|
||
the modules are platform-specific. In that case, it may not even be possible to
|
||
import all of the modules at the top of the file. In this case, importing the
|
||
correct modules in the corresponding platform-specific code is a good option.</p>
|
||
<p>Only move imports into a local scope, such as inside a function definition, if
|
||
it’s necessary to solve a problem such as avoiding a circular import or are
|
||
trying to reduce the initialization time of a module. This technique is
|
||
especially helpful if many of the imports are unnecessary depending on how the
|
||
program executes. You may also want to move imports into a function if the
|
||
modules are only ever used in that function. Note that loading a module the
|
||
first time may be expensive because of the one time initialization of the
|
||
module, but loading a module multiple times is virtually free, costing only a
|
||
couple of dictionary lookups. Even if the module name has gone out of scope,
|
||
the module is probably available in <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>.</p>
|
||
</div>
|
||
<div class="section" id="why-are-default-values-shared-between-objects">
|
||
<h3><a class="toc-backref" href="#id13">Why are default values shared between objects?</a><a class="headerlink" href="#why-are-default-values-shared-between-objects" title="Permalink to this headline">¶</a></h3>
|
||
<p>This type of bug commonly bites neophyte programmers. Consider this function:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">mydict</span><span class="o">=</span><span class="p">{}):</span> <span class="c1"># Danger: shared reference to one dict for all calls</span>
|
||
<span class="o">...</span> <span class="n">compute</span> <span class="n">something</span> <span class="o">...</span>
|
||
<span class="n">mydict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
|
||
<span class="k">return</span> <span class="n">mydict</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The first time you call this function, <code class="docutils literal notranslate"><span class="pre">mydict</span></code> contains a single item. The
|
||
second time, <code class="docutils literal notranslate"><span class="pre">mydict</span></code> contains two items because when <code class="docutils literal notranslate"><span class="pre">foo()</span></code> begins
|
||
executing, <code class="docutils literal notranslate"><span class="pre">mydict</span></code> starts out with an item already in it.</p>
|
||
<p>It is often expected that a function call creates new objects for default
|
||
values. This is not what happens. Default values are created exactly once, when
|
||
the function is defined. If that object is changed, like the dictionary in this
|
||
example, subsequent calls to the function will refer to this changed object.</p>
|
||
<p>By definition, immutable objects such as numbers, strings, tuples, and <code class="docutils literal notranslate"><span class="pre">None</span></code>,
|
||
are safe from change. Changes to mutable objects such as dictionaries, lists,
|
||
and class instances can lead to confusion.</p>
|
||
<p>Because of this feature, it is good programming practice to not use mutable
|
||
objects as default values. Instead, use <code class="docutils literal notranslate"><span class="pre">None</span></code> as the default value and
|
||
inside the function, check if the parameter is <code class="docutils literal notranslate"><span class="pre">None</span></code> and create a new
|
||
list/dictionary/whatever if it is. For example, don’t write:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">mydict</span><span class="o">=</span><span class="p">{}):</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>but:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">mydict</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
|
||
<span class="k">if</span> <span class="n">mydict</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
|
||
<span class="n">mydict</span> <span class="o">=</span> <span class="p">{}</span> <span class="c1"># create a new dict for local namespace</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This feature can be useful. When you have a function that’s time-consuming to
|
||
compute, a common technique is to cache the parameters and the resulting value
|
||
of each call to the function, and return the cached value if the same value is
|
||
requested again. This is called “memoizing”, and can be implemented like this:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Callers can only provide two parameters and optionally pass _cache by keyword</span>
|
||
<span class="k">def</span> <span class="nf">expensive</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="n">_cache</span><span class="o">=</span><span class="p">{}):</span>
|
||
<span class="k">if</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="ow">in</span> <span class="n">_cache</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="n">_cache</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="c1"># Calculate the value</span>
|
||
<span class="n">result</span> <span class="o">=</span> <span class="o">...</span> <span class="n">expensive</span> <span class="n">computation</span> <span class="o">...</span>
|
||
<span class="n">_cache</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="n">result</span> <span class="c1"># Store result in the cache</span>
|
||
<span class="k">return</span> <span class="n">result</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You could use a global variable containing a dictionary instead of the default
|
||
value; it’s a matter of taste.</p>
|
||
</div>
|
||
<div class="section" id="how-can-i-pass-optional-or-keyword-parameters-from-one-function-to-another">
|
||
<h3><a class="toc-backref" href="#id14">How can I pass optional or keyword parameters from one function to another?</a><a class="headerlink" href="#how-can-i-pass-optional-or-keyword-parameters-from-one-function-to-another" title="Permalink to this headline">¶</a></h3>
|
||
<p>Collect the arguments using the <code class="docutils literal notranslate"><span class="pre">*</span></code> and <code class="docutils literal notranslate"><span class="pre">**</span></code> specifiers in the function’s
|
||
parameter list; this gives you the positional arguments as a tuple and the
|
||
keyword arguments as a dictionary. You can then pass these arguments when
|
||
calling another function by using <code class="docutils literal notranslate"><span class="pre">*</span></code> and <code class="docutils literal notranslate"><span class="pre">**</span></code>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
||
<span class="o">...</span>
|
||
<span class="n">kwargs</span><span class="p">[</span><span class="s1">'width'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'14.3c'</span>
|
||
<span class="o">...</span>
|
||
<span class="n">g</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="what-is-the-difference-between-arguments-and-parameters">
|
||
<span id="faq-argument-vs-parameter"></span><span id="index-1"></span><h3><a class="toc-backref" href="#id15">What is the difference between arguments and parameters?</a><a class="headerlink" href="#what-is-the-difference-between-arguments-and-parameters" title="Permalink to this headline">¶</a></h3>
|
||
<p><a class="reference internal" href="../glossary.html#term-parameter"><span class="xref std std-term">Parameters</span></a> are defined by the names that appear in a
|
||
function definition, whereas <a class="reference internal" href="../glossary.html#term-argument"><span class="xref std std-term">arguments</span></a> are the values
|
||
actually passed to a function when calling it. Parameters define what types of
|
||
arguments a function can accept. For example, given the function definition:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">foo</span><span class="p">,</span> <span class="n">bar</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
||
<span class="k">pass</span>
|
||
</pre></div>
|
||
</div>
|
||
<p><em>foo</em>, <em>bar</em> and <em>kwargs</em> are parameters of <code class="docutils literal notranslate"><span class="pre">func</span></code>. However, when calling
|
||
<code class="docutils literal notranslate"><span class="pre">func</span></code>, for example:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">func</span><span class="p">(</span><span class="mi">42</span><span class="p">,</span> <span class="n">bar</span><span class="o">=</span><span class="mi">314</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="n">somevar</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>the values <code class="docutils literal notranslate"><span class="pre">42</span></code>, <code class="docutils literal notranslate"><span class="pre">314</span></code>, and <code class="docutils literal notranslate"><span class="pre">somevar</span></code> are arguments.</p>
|
||
</div>
|
||
<div class="section" id="why-did-changing-list-y-also-change-list-x">
|
||
<h3><a class="toc-backref" href="#id16">Why did changing list ‘y’ also change list ‘x’?</a><a class="headerlink" href="#why-did-changing-list-y-also-change-list-x" title="Permalink to this headline">¶</a></h3>
|
||
<p>If you wrote code like:</p>
|
||
<div class="highlight-python3 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="gp">>>> </span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span>
|
||
<span class="gp">>>> </span><span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">y</span>
|
||
<span class="go">[10]</span>
|
||
<span class="gp">>>> </span><span class="n">x</span>
|
||
<span class="go">[10]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>you might be wondering why appending an element to <code class="docutils literal notranslate"><span class="pre">y</span></code> changed <code class="docutils literal notranslate"><span class="pre">x</span></code> too.</p>
|
||
<p>There are two factors that produce this result:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>Variables are simply names that refer to objects. Doing <code class="docutils literal notranslate"><span class="pre">y</span> <span class="pre">=</span> <span class="pre">x</span></code> doesn’t
|
||
create a copy of the list – it creates a new variable <code class="docutils literal notranslate"><span class="pre">y</span></code> that refers to
|
||
the same object <code class="docutils literal notranslate"><span class="pre">x</span></code> refers to. This means that there is only one object
|
||
(the list), and both <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code> refer to it.</p></li>
|
||
<li><p>Lists are <a class="reference internal" href="../glossary.html#term-mutable"><span class="xref std std-term">mutable</span></a>, which means that you can change their content.</p></li>
|
||
</ol>
|
||
<p>After the call to <code class="xref py py-meth docutils literal notranslate"><span class="pre">append()</span></code>, the content of the mutable object has
|
||
changed from <code class="docutils literal notranslate"><span class="pre">[]</span></code> to <code class="docutils literal notranslate"><span class="pre">[10]</span></code>. Since both the variables refer to the same
|
||
object, using either name accesses the modified value <code class="docutils literal notranslate"><span class="pre">[10]</span></code>.</p>
|
||
<p>If we instead assign an immutable object to <code class="docutils literal notranslate"><span class="pre">x</span></code>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="mi">5</span> <span class="c1"># ints are immutable</span>
|
||
<span class="gp">>>> </span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span>
|
||
<span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># 5 can't be mutated, we are creating a new object here</span>
|
||
<span class="gp">>>> </span><span class="n">x</span>
|
||
<span class="go">6</span>
|
||
<span class="gp">>>> </span><span class="n">y</span>
|
||
<span class="go">5</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>we can see that in this case <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code> are not equal anymore. This is
|
||
because integers are <a class="reference internal" href="../glossary.html#term-immutable"><span class="xref std std-term">immutable</span></a>, and when we do <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">x</span> <span class="pre">+</span> <span class="pre">1</span></code> we are not
|
||
mutating the int <code class="docutils literal notranslate"><span class="pre">5</span></code> by incrementing its value; instead, we are creating a
|
||
new object (the int <code class="docutils literal notranslate"><span class="pre">6</span></code>) and assigning it to <code class="docutils literal notranslate"><span class="pre">x</span></code> (that is, changing which
|
||
object <code class="docutils literal notranslate"><span class="pre">x</span></code> refers to). After this assignment we have two objects (the ints
|
||
<code class="docutils literal notranslate"><span class="pre">6</span></code> and <code class="docutils literal notranslate"><span class="pre">5</span></code>) and two variables that refer to them (<code class="docutils literal notranslate"><span class="pre">x</span></code> now refers to
|
||
<code class="docutils literal notranslate"><span class="pre">6</span></code> but <code class="docutils literal notranslate"><span class="pre">y</span></code> still refers to <code class="docutils literal notranslate"><span class="pre">5</span></code>).</p>
|
||
<p>Some operations (for example <code class="docutils literal notranslate"><span class="pre">y.append(10)</span></code> and <code class="docutils literal notranslate"><span class="pre">y.sort()</span></code>) mutate the
|
||
object, whereas superficially similar operations (for example <code class="docutils literal notranslate"><span class="pre">y</span> <span class="pre">=</span> <span class="pre">y</span> <span class="pre">+</span> <span class="pre">[10]</span></code>
|
||
and <code class="docutils literal notranslate"><span class="pre">sorted(y)</span></code>) create a new object. In general in Python (and in all cases
|
||
in the standard library) a method that mutates an object will return <code class="docutils literal notranslate"><span class="pre">None</span></code>
|
||
to help avoid getting the two types of operations confused. So if you
|
||
mistakenly write <code class="docutils literal notranslate"><span class="pre">y.sort()</span></code> thinking it will give you a sorted copy of <code class="docutils literal notranslate"><span class="pre">y</span></code>,
|
||
you’ll instead end up with <code class="docutils literal notranslate"><span class="pre">None</span></code>, which will likely cause your program to
|
||
generate an easily diagnosed error.</p>
|
||
<p>However, there is one class of operations where the same operation sometimes
|
||
has different behaviors with different types: the augmented assignment
|
||
operators. For example, <code class="docutils literal notranslate"><span class="pre">+=</span></code> mutates lists but not tuples or ints (<code class="docutils literal notranslate"><span class="pre">a_list</span>
|
||
<span class="pre">+=</span> <span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">a_list.extend([1,</span> <span class="pre">2,</span> <span class="pre">3])</span></code> and mutates
|
||
<code class="docutils literal notranslate"><span class="pre">a_list</span></code>, whereas <code class="docutils literal notranslate"><span class="pre">some_tuple</span> <span class="pre">+=</span> <span class="pre">(1,</span> <span class="pre">2,</span> <span class="pre">3)</span></code> and <code class="docutils literal notranslate"><span class="pre">some_int</span> <span class="pre">+=</span> <span class="pre">1</span></code> create
|
||
new objects).</p>
|
||
<p>In other words:</p>
|
||
<ul class="simple">
|
||
<li><p>If we have a mutable object (<a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a>, <a class="reference internal" href="../library/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="../library/stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a>,
|
||
etc.), we can use some specific operations to mutate it and all the variables
|
||
that refer to it will see the change.</p></li>
|
||
<li><p>If we have an immutable object (<a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a>,
|
||
etc.), all the variables that refer to it will always see the same value,
|
||
but operations that transform that value into a new value always return a new
|
||
object.</p></li>
|
||
</ul>
|
||
<p>If you want to know if two variables refer to the same object or not, you can
|
||
use the <a class="reference internal" href="../reference/expressions.html#is"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">is</span></code></a> operator, or the built-in function <a class="reference internal" href="../library/functions.html#id" title="id"><code class="xref py py-func docutils literal notranslate"><span class="pre">id()</span></code></a>.</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-write-a-function-with-output-parameters-call-by-reference">
|
||
<h3><a class="toc-backref" href="#id17">How do I write a function with output parameters (call by reference)?</a><a class="headerlink" href="#how-do-i-write-a-function-with-output-parameters-call-by-reference" title="Permalink to this headline">¶</a></h3>
|
||
<p>Remember that arguments are passed by assignment in Python. Since assignment
|
||
just creates references to objects, there’s no alias between an argument name in
|
||
the caller and callee, and so no call-by-reference per se. You can achieve the
|
||
desired effect in a number of ways.</p>
|
||
<ol class="arabic">
|
||
<li><p>By returning a tuple of the results:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">func2</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
||
<span class="n">a</span> <span class="o">=</span> <span class="s1">'new-value'</span> <span class="c1"># a and b are local names</span>
|
||
<span class="n">b</span> <span class="o">=</span> <span class="n">b</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># assigned to new objects</span>
|
||
<span class="k">return</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="c1"># return new values</span>
|
||
|
||
<span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="s1">'old-value'</span><span class="p">,</span> <span class="mi">99</span>
|
||
<span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">func2</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="nb">print</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="c1"># output: new-value 100</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This is almost always the clearest solution.</p>
|
||
</li>
|
||
<li><p>By using global variables. This isn’t thread-safe, and is not recommended.</p></li>
|
||
<li><p>By passing a mutable (changeable in-place) object:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">func1</span><span class="p">(</span><span class="n">a</span><span class="p">):</span>
|
||
<span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'new-value'</span> <span class="c1"># 'a' references a mutable list</span>
|
||
<span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># changes a shared object</span>
|
||
|
||
<span class="n">args</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'old-value'</span><span class="p">,</span> <span class="mi">99</span><span class="p">]</span>
|
||
<span class="n">func1</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="c1"># output: new-value 100</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
<li><p>By passing in a dictionary that gets mutated:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">func3</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
|
||
<span class="n">args</span><span class="p">[</span><span class="s1">'a'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'new-value'</span> <span class="c1"># args is a mutable dictionary</span>
|
||
<span class="n">args</span><span class="p">[</span><span class="s1">'b'</span><span class="p">]</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="s1">'b'</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># change it in-place</span>
|
||
|
||
<span class="n">args</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'a'</span><span class="p">:</span> <span class="s1">'old-value'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">:</span> <span class="mi">99</span><span class="p">}</span>
|
||
<span class="n">func3</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="s1">'a'</span><span class="p">],</span> <span class="n">args</span><span class="p">[</span><span class="s1">'b'</span><span class="p">])</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
<li><p>Or bundle up values in a class instance:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">callByRef</span><span class="p">:</span>
|
||
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">args</span><span class="p">):</span>
|
||
<span class="k">for</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="ow">in</span> <span class="n">args</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
|
||
<span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
|
||
|
||
<span class="k">def</span> <span class="nf">func4</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
|
||
<span class="n">args</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="s1">'new-value'</span> <span class="c1"># args is a mutable callByRef</span>
|
||
<span class="n">args</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">b</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># change object in-place</span>
|
||
|
||
<span class="n">args</span> <span class="o">=</span> <span class="n">callByRef</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="s1">'old-value'</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">99</span><span class="p">)</span>
|
||
<span class="n">func4</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">a</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">b</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>There’s almost never a good reason to get this complicated.</p>
|
||
</li>
|
||
</ol>
|
||
<p>Your best choice is to return a tuple containing the multiple results.</p>
|
||
</div>
|
||
<div class="section" id="how-do-you-make-a-higher-order-function-in-python">
|
||
<h3><a class="toc-backref" href="#id18">How do you make a higher order function in Python?</a><a class="headerlink" href="#how-do-you-make-a-higher-order-function-in-python" title="Permalink to this headline">¶</a></h3>
|
||
<p>You have two choices: you can use nested scopes or you can use callable objects.
|
||
For example, suppose you wanted to define <code class="docutils literal notranslate"><span class="pre">linear(a,b)</span></code> which returns a
|
||
function <code class="docutils literal notranslate"><span class="pre">f(x)</span></code> that computes the value <code class="docutils literal notranslate"><span class="pre">a*x+b</span></code>. Using nested scopes:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">linear</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
||
<span class="k">def</span> <span class="nf">result</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="n">a</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <span class="n">b</span>
|
||
<span class="k">return</span> <span class="n">result</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Or using a callable object:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">linear</span><span class="p">:</span>
|
||
|
||
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">a</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span>
|
||
|
||
<span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">a</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">b</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>In both cases,</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">taxes</span> <span class="o">=</span> <span class="n">linear</span><span class="p">(</span><span class="mf">0.3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>gives a callable object where <code class="docutils literal notranslate"><span class="pre">taxes(10e6)</span> <span class="pre">==</span> <span class="pre">0.3</span> <span class="pre">*</span> <span class="pre">10e6</span> <span class="pre">+</span> <span class="pre">2</span></code>.</p>
|
||
<p>The callable object approach has the disadvantage that it is a bit slower and
|
||
results in slightly longer code. However, note that a collection of callables
|
||
can share their signature via inheritance:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">exponential</span><span class="p">(</span><span class="n">linear</span><span class="p">):</span>
|
||
<span class="c1"># __init__ inherited</span>
|
||
<span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">a</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span> <span class="o">**</span> <span class="bp">self</span><span class="o">.</span><span class="n">b</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Object can encapsulate state for several methods:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">counter</span><span class="p">:</span>
|
||
|
||
<span class="n">value</span> <span class="o">=</span> <span class="mi">0</span>
|
||
|
||
<span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">x</span>
|
||
|
||
<span class="k">def</span> <span class="nf">up</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">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="mi">1</span>
|
||
|
||
<span class="k">def</span> <span class="nf">down</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">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">-</span> <span class="mi">1</span>
|
||
|
||
<span class="n">count</span> <span class="o">=</span> <span class="n">counter</span><span class="p">()</span>
|
||
<span class="n">inc</span><span class="p">,</span> <span class="n">dec</span><span class="p">,</span> <span class="n">reset</span> <span class="o">=</span> <span class="n">count</span><span class="o">.</span><span class="n">up</span><span class="p">,</span> <span class="n">count</span><span class="o">.</span><span class="n">down</span><span class="p">,</span> <span class="n">count</span><span class="o">.</span><span class="n">set</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Here <code class="docutils literal notranslate"><span class="pre">inc()</span></code>, <code class="docutils literal notranslate"><span class="pre">dec()</span></code> and <code class="docutils literal notranslate"><span class="pre">reset()</span></code> act like functions which share the
|
||
same counting variable.</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-copy-an-object-in-python">
|
||
<h3><a class="toc-backref" href="#id19">How do I copy an object in Python?</a><a class="headerlink" href="#how-do-i-copy-an-object-in-python" title="Permalink to this headline">¶</a></h3>
|
||
<p>In general, try <a class="reference internal" href="../library/copy.html#copy.copy" title="copy.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy.copy()</span></code></a> or <a class="reference internal" href="../library/copy.html#copy.deepcopy" title="copy.deepcopy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy.deepcopy()</span></code></a> for the general case.
|
||
Not all objects can be copied, but most can.</p>
|
||
<p>Some objects can be copied more easily. Dictionaries have a <a class="reference internal" href="../library/stdtypes.html#dict.copy" title="dict.copy"><code class="xref py py-meth docutils literal notranslate"><span class="pre">copy()</span></code></a>
|
||
method:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">newdict</span> <span class="o">=</span> <span class="n">olddict</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Sequences can be copied by slicing:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">new_l</span> <span class="o">=</span> <span class="n">l</span><span class="p">[:]</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="how-can-i-find-the-methods-or-attributes-of-an-object">
|
||
<h3><a class="toc-backref" href="#id20">How can I find the methods or attributes of an object?</a><a class="headerlink" href="#how-can-i-find-the-methods-or-attributes-of-an-object" title="Permalink to this headline">¶</a></h3>
|
||
<p>For an instance x of a user-defined class, <code class="docutils literal notranslate"><span class="pre">dir(x)</span></code> returns an alphabetized
|
||
list of the names containing the instance attributes and methods and attributes
|
||
defined by its class.</p>
|
||
</div>
|
||
<div class="section" id="how-can-my-code-discover-the-name-of-an-object">
|
||
<h3><a class="toc-backref" href="#id21">How can my code discover the name of an object?</a><a class="headerlink" href="#how-can-my-code-discover-the-name-of-an-object" title="Permalink to this headline">¶</a></h3>
|
||
<p>Generally speaking, it can’t, because objects don’t really have names.
|
||
Essentially, assignment always binds a name to a value; The same is true of
|
||
<code class="docutils literal notranslate"><span class="pre">def</span></code> and <code class="docutils literal notranslate"><span class="pre">class</span></code> statements, but in that case the value is a
|
||
callable. Consider the following code:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="k">pass</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </span><span class="n">B</span> <span class="o">=</span> <span class="n">A</span>
|
||
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">B</span><span class="p">()</span>
|
||
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span>
|
||
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
|
||
<span class="go"><__main__.A object at 0x16D07CC></span>
|
||
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
|
||
<span class="go"><__main__.A object at 0x16D07CC></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Arguably the class has a name: even though it is bound to two names and invoked
|
||
through the name B the created instance is still reported as an instance of
|
||
class A. However, it is impossible to say whether the instance’s name is a or
|
||
b, since both names are bound to the same value.</p>
|
||
<p>Generally speaking it should not be necessary for your code to “know the names”
|
||
of particular values. Unless you are deliberately writing introspective
|
||
programs, this is usually an indication that a change of approach might be
|
||
beneficial.</p>
|
||
<p>In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to
|
||
this question:</p>
|
||
<blockquote>
|
||
<div><p>The same way as you get the name of that cat you found on your porch: the cat
|
||
(object) itself cannot tell you its name, and it doesn’t really care – so
|
||
the only way to find out what it’s called is to ask all your neighbours
|
||
(namespaces) if it’s their cat (object)…</p>
|
||
<p>….and don’t be surprised if you’ll find that it’s known by many names, or
|
||
no name at all!</p>
|
||
</div></blockquote>
|
||
</div>
|
||
<div class="section" id="what-s-up-with-the-comma-operator-s-precedence">
|
||
<h3><a class="toc-backref" href="#id22">What’s up with the comma operator’s precedence?</a><a class="headerlink" href="#what-s-up-with-the-comma-operator-s-precedence" title="Permalink to this headline">¶</a></h3>
|
||
<p>Comma is not an operator in Python. Consider this session:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s2">"a"</span> <span class="ow">in</span> <span class="s2">"b"</span><span class="p">,</span> <span class="s2">"a"</span>
|
||
<span class="go">(False, 'a')</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Since the comma is not an operator, but a separator between expressions the
|
||
above is evaluated as if you had entered:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="s2">"a"</span> <span class="ow">in</span> <span class="s2">"b"</span><span class="p">),</span> <span class="s2">"a"</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>not:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="s2">"a"</span> <span class="ow">in</span> <span class="p">(</span><span class="s2">"b"</span><span class="p">,</span> <span class="s2">"a"</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The same is true of the various assignment operators (<code class="docutils literal notranslate"><span class="pre">=</span></code>, <code class="docutils literal notranslate"><span class="pre">+=</span></code> etc). They
|
||
are not truly operators but syntactic delimiters in assignment statements.</p>
|
||
</div>
|
||
<div class="section" id="is-there-an-equivalent-of-c-s-ternary-operator">
|
||
<h3><a class="toc-backref" href="#id23">Is there an equivalent of C’s “?:” ternary operator?</a><a class="headerlink" href="#is-there-an-equivalent-of-c-s-ternary-operator" title="Permalink to this headline">¶</a></h3>
|
||
<p>Yes, there is. The syntax is as follows:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">on_true</span><span class="p">]</span> <span class="k">if</span> <span class="p">[</span><span class="n">expression</span><span class="p">]</span> <span class="k">else</span> <span class="p">[</span><span class="n">on_false</span><span class="p">]</span>
|
||
|
||
<span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">25</span>
|
||
<span class="n">small</span> <span class="o">=</span> <span class="n">x</span> <span class="k">if</span> <span class="n">x</span> <span class="o"><</span> <span class="n">y</span> <span class="k">else</span> <span class="n">y</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Before this syntax was introduced in Python 2.5, a common idiom was to use
|
||
logical operators:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">expression</span><span class="p">]</span> <span class="ow">and</span> <span class="p">[</span><span class="n">on_true</span><span class="p">]</span> <span class="ow">or</span> <span class="p">[</span><span class="n">on_false</span><span class="p">]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>However, this idiom is unsafe, as it can give wrong results when <em>on_true</em>
|
||
has a false boolean value. Therefore, it is always better to use
|
||
the <code class="docutils literal notranslate"><span class="pre">...</span> <span class="pre">if</span> <span class="pre">...</span> <span class="pre">else</span> <span class="pre">...</span></code> form.</p>
|
||
</div>
|
||
<div class="section" id="is-it-possible-to-write-obfuscated-one-liners-in-python">
|
||
<h3><a class="toc-backref" href="#id24">Is it possible to write obfuscated one-liners in Python?</a><a class="headerlink" href="#is-it-possible-to-write-obfuscated-one-liners-in-python" title="Permalink to this headline">¶</a></h3>
|
||
<p>Yes. Usually this is done by nesting <a class="reference internal" href="../reference/expressions.html#lambda"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code></a> within
|
||
<code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code>. See the following three examples, due to Ulf Bartelt:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">reduce</span>
|
||
|
||
<span class="c1"># Primes < 1000</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">y</span><span class="p">:</span><span class="n">y</span><span class="o">*</span><span class="n">reduce</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">!=</span><span class="mi">0</span><span class="p">,</span>
|
||
<span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="o">=</span><span class="n">y</span><span class="p">:</span><span class="n">y</span><span class="o">%</span><span class="n">x</span><span class="p">,</span><span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="nb">int</span><span class="p">(</span><span class="nb">pow</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="mf">0.5</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">))),</span><span class="mi">1</span><span class="p">),</span><span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">1000</span><span class="p">)))))</span>
|
||
|
||
<span class="c1"># First 10 Fibonacci numbers</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">f</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">f</span><span class="p">:(</span><span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="n">f</span><span class="p">)</span><span class="o">+</span><span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="n">f</span><span class="p">))</span> <span class="k">if</span> <span class="n">x</span><span class="o">></span><span class="mi">1</span> <span class="k">else</span> <span class="mi">1</span><span class="p">:</span>
|
||
<span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">f</span><span class="p">),</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))))</span>
|
||
|
||
<span class="c1"># Mandelbrot set</span>
|
||
<span class="nb">print</span><span class="p">((</span><span class="k">lambda</span> <span class="n">Ru</span><span class="p">,</span><span class="n">Ro</span><span class="p">,</span><span class="n">Iu</span><span class="p">,</span><span class="n">Io</span><span class="p">,</span><span class="n">IM</span><span class="p">,</span><span class="n">Sx</span><span class="p">,</span><span class="n">Sy</span><span class="p">:</span><span class="n">reduce</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">,</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">y</span><span class="p">,</span>
|
||
<span class="n">Iu</span><span class="o">=</span><span class="n">Iu</span><span class="p">,</span><span class="n">Io</span><span class="o">=</span><span class="n">Io</span><span class="p">,</span><span class="n">Ru</span><span class="o">=</span><span class="n">Ru</span><span class="p">,</span><span class="n">Ro</span><span class="o">=</span><span class="n">Ro</span><span class="p">,</span><span class="n">Sy</span><span class="o">=</span><span class="n">Sy</span><span class="p">,</span><span class="n">L</span><span class="o">=</span><span class="k">lambda</span> <span class="n">yc</span><span class="p">,</span><span class="n">Iu</span><span class="o">=</span><span class="n">Iu</span><span class="p">,</span><span class="n">Io</span><span class="o">=</span><span class="n">Io</span><span class="p">,</span><span class="n">Ru</span><span class="o">=</span><span class="n">Ru</span><span class="p">,</span><span class="n">Ro</span><span class="o">=</span><span class="n">Ro</span><span class="p">,</span><span class="n">i</span><span class="o">=</span><span class="n">IM</span><span class="p">,</span>
|
||
<span class="n">Sx</span><span class="o">=</span><span class="n">Sx</span><span class="p">,</span><span class="n">Sy</span><span class="o">=</span><span class="n">Sy</span><span class="p">:</span><span class="n">reduce</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">,</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">xc</span><span class="o">=</span><span class="n">Ru</span><span class="p">,</span><span class="n">yc</span><span class="o">=</span><span class="n">yc</span><span class="p">,</span><span class="n">Ru</span><span class="o">=</span><span class="n">Ru</span><span class="p">,</span><span class="n">Ro</span><span class="o">=</span><span class="n">Ro</span><span class="p">,</span>
|
||
<span class="n">i</span><span class="o">=</span><span class="n">i</span><span class="p">,</span><span class="n">Sx</span><span class="o">=</span><span class="n">Sx</span><span class="p">,</span><span class="n">F</span><span class="o">=</span><span class="k">lambda</span> <span class="n">xc</span><span class="p">,</span><span class="n">yc</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="n">k</span><span class="p">,</span><span class="n">f</span><span class="o">=</span><span class="k">lambda</span> <span class="n">xc</span><span class="p">,</span><span class="n">yc</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="n">k</span><span class="p">,</span><span class="n">f</span><span class="p">:(</span><span class="n">k</span><span class="o"><=</span><span class="mi">0</span><span class="p">)</span><span class="ow">or</span> <span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="o">*</span><span class="n">y</span>
|
||
<span class="o">>=</span><span class="mf">4.0</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">1</span><span class="o">+</span><span class="n">f</span><span class="p">(</span><span class="n">xc</span><span class="p">,</span><span class="n">yc</span><span class="p">,</span><span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="o">-</span><span class="n">y</span><span class="o">*</span><span class="n">y</span><span class="o">+</span><span class="n">xc</span><span class="p">,</span><span class="mf">2.0</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">+</span><span class="n">yc</span><span class="p">,</span><span class="n">k</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="n">f</span><span class="p">):</span><span class="n">f</span><span class="p">(</span><span class="n">xc</span><span class="p">,</span><span class="n">yc</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="n">k</span><span class="p">,</span><span class="n">f</span><span class="p">):</span><span class="nb">chr</span><span class="p">(</span>
|
||
<span class="mi">64</span><span class="o">+</span><span class="n">F</span><span class="p">(</span><span class="n">Ru</span><span class="o">+</span><span class="n">x</span><span class="o">*</span><span class="p">(</span><span class="n">Ro</span><span class="o">-</span><span class="n">Ru</span><span class="p">)</span><span class="o">/</span><span class="n">Sx</span><span class="p">,</span><span class="n">yc</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">i</span><span class="p">)),</span><span class="nb">range</span><span class="p">(</span><span class="n">Sx</span><span class="p">))):</span><span class="n">L</span><span class="p">(</span><span class="n">Iu</span><span class="o">+</span><span class="n">y</span><span class="o">*</span><span class="p">(</span><span class="n">Io</span><span class="o">-</span><span class="n">Iu</span><span class="p">)</span><span class="o">/</span><span class="n">Sy</span><span class="p">),</span><span class="nb">range</span><span class="p">(</span><span class="n">Sy</span>
|
||
<span class="p">))))(</span><span class="o">-</span><span class="mf">2.1</span><span class="p">,</span> <span class="mf">0.7</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.2</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">80</span><span class="p">,</span> <span class="mi">24</span><span class="p">))</span>
|
||
<span class="c1"># \___ ___/ \___ ___/ | | |__ lines on screen</span>
|
||
<span class="c1"># V V | |______ columns on screen</span>
|
||
<span class="c1"># | | |__________ maximum of "iterations"</span>
|
||
<span class="c1"># | |_________________ range on y axis</span>
|
||
<span class="c1"># |____________________________ range on x axis</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Don’t try this at home, kids!</p>
|
||
</div>
|
||
<div class="section" id="what-does-the-slash-in-the-parameter-list-of-a-function-mean">
|
||
<span id="faq-positional-only-arguments"></span><h3><a class="toc-backref" href="#id25">What does the slash(/) in the parameter list of a function mean?</a><a class="headerlink" href="#what-does-the-slash-in-the-parameter-list-of-a-function-mean" title="Permalink to this headline">¶</a></h3>
|
||
<p>A slash in the argument list of a function denotes that the parameters prior to
|
||
it are positional-only. Positional-only parameters are the ones without an
|
||
externally-usable name. Upon calling a function that accepts positional-only
|
||
parameters, arguments are mapped to parameters based solely on their position.
|
||
For example, <a class="reference internal" href="../library/functions.html#pow" title="pow"><code class="xref py py-func docutils literal notranslate"><span class="pre">pow()</span></code></a> is a function that accepts positional-only parameters.
|
||
Its documentation looks like this:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">help</span><span class="p">(</span><span class="nb">pow</span><span class="p">)</span>
|
||
<span class="go">Help on built-in function pow in module builtins:</span>
|
||
|
||
<span class="go">pow(x, y, z=None, /)</span>
|
||
<span class="go"> Equivalent to x**y (with two arguments) or x**y % z (with three arguments)</span>
|
||
|
||
<span class="go"> Some types, such as ints, are able to use a more efficient algorithm when</span>
|
||
<span class="go"> invoked using the three argument form.</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The slash at the end of the parameter list means that all three parameters are
|
||
positional-only. Thus, calling <a class="reference internal" href="../library/functions.html#pow" title="pow"><code class="xref py py-func docutils literal notranslate"><span class="pre">pow()</span></code></a> with keyword aguments would lead to
|
||
an error:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
|
||
<span class="gr">TypeError</span>: <span class="n">pow() takes no keyword arguments</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Note that as of this writing this is only documentational and no valid syntax
|
||
in Python, although there is <span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0570"><strong>PEP 570</strong></a>, which proposes a syntax for
|
||
position-only parameters in Python.</p>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="numbers-and-strings">
|
||
<h2><a class="toc-backref" href="#id26">Numbers and strings</a><a class="headerlink" href="#numbers-and-strings" title="Permalink to this headline">¶</a></h2>
|
||
<div class="section" id="how-do-i-specify-hexadecimal-and-octal-integers">
|
||
<h3><a class="toc-backref" href="#id27">How do I specify hexadecimal and octal integers?</a><a class="headerlink" href="#how-do-i-specify-hexadecimal-and-octal-integers" title="Permalink to this headline">¶</a></h3>
|
||
<p>To specify an octal digit, precede the octal value with a zero, and then a lower
|
||
or uppercase “o”. For example, to set the variable “a” to the octal value “10”
|
||
(8 in decimal), type:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="mo">0o10</span>
|
||
<span class="gp">>>> </span><span class="n">a</span>
|
||
<span class="go">8</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero,
|
||
and then a lower or uppercase “x”. Hexadecimal digits can be specified in lower
|
||
or uppercase. For example, in the Python interpreter:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="mh">0xa5</span>
|
||
<span class="gp">>>> </span><span class="n">a</span>
|
||
<span class="go">165</span>
|
||
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="mh">0XB2</span>
|
||
<span class="gp">>>> </span><span class="n">b</span>
|
||
<span class="go">178</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="why-does-22-10-return-3">
|
||
<h3><a class="toc-backref" href="#id28">Why does -22 // 10 return -3?</a><a class="headerlink" href="#why-does-22-10-return-3" title="Permalink to this headline">¶</a></h3>
|
||
<p>It’s primarily driven by the desire that <code class="docutils literal notranslate"><span class="pre">i</span> <span class="pre">%</span> <span class="pre">j</span></code> have the same sign as <code class="docutils literal notranslate"><span class="pre">j</span></code>.
|
||
If you want that, and also want:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">i</span> <span class="o">==</span> <span class="p">(</span><span class="n">i</span> <span class="o">//</span> <span class="n">j</span><span class="p">)</span> <span class="o">*</span> <span class="n">j</span> <span class="o">+</span> <span class="p">(</span><span class="n">i</span> <span class="o">%</span> <span class="n">j</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>then integer division has to return the floor. C also requires that identity to
|
||
hold, and then compilers that truncate <code class="docutils literal notranslate"><span class="pre">i</span> <span class="pre">//</span> <span class="pre">j</span></code> need to make <code class="docutils literal notranslate"><span class="pre">i</span> <span class="pre">%</span> <span class="pre">j</span></code> have
|
||
the same sign as <code class="docutils literal notranslate"><span class="pre">i</span></code>.</p>
|
||
<p>There are few real use cases for <code class="docutils literal notranslate"><span class="pre">i</span> <span class="pre">%</span> <span class="pre">j</span></code> when <code class="docutils literal notranslate"><span class="pre">j</span></code> is negative. When <code class="docutils literal notranslate"><span class="pre">j</span></code>
|
||
is positive, there are many, and in virtually all of them it’s more useful for
|
||
<code class="docutils literal notranslate"><span class="pre">i</span> <span class="pre">%</span> <span class="pre">j</span></code> to be <code class="docutils literal notranslate"><span class="pre">>=</span> <span class="pre">0</span></code>. If the clock says 10 now, what did it say 200 hours
|
||
ago? <code class="docutils literal notranslate"><span class="pre">-190</span> <span class="pre">%</span> <span class="pre">12</span> <span class="pre">==</span> <span class="pre">2</span></code> is useful; <code class="docutils literal notranslate"><span class="pre">-190</span> <span class="pre">%</span> <span class="pre">12</span> <span class="pre">==</span> <span class="pre">-10</span></code> is a bug waiting to
|
||
bite.</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-convert-a-string-to-a-number">
|
||
<h3><a class="toc-backref" href="#id29">How do I convert a string to a number?</a><a class="headerlink" href="#how-do-i-convert-a-string-to-a-number" title="Permalink to this headline">¶</a></h3>
|
||
<p>For integers, use the built-in <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> type constructor, e.g. <code class="docutils literal notranslate"><span class="pre">int('144')</span>
|
||
<span class="pre">==</span> <span class="pre">144</span></code>. Similarly, <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a> converts to floating-point,
|
||
e.g. <code class="docutils literal notranslate"><span class="pre">float('144')</span> <span class="pre">==</span> <span class="pre">144.0</span></code>.</p>
|
||
<p>By default, these interpret the number as decimal, so that <code class="docutils literal notranslate"><span class="pre">int('0144')</span> <span class="pre">==</span>
|
||
<span class="pre">144</span></code> and <code class="docutils literal notranslate"><span class="pre">int('0x144')</span></code> raises <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>. <code class="docutils literal notranslate"><span class="pre">int(string,</span> <span class="pre">base)</span></code> takes
|
||
the base to convert from as a second optional argument, so <code class="docutils literal notranslate"><span class="pre">int('0x144',</span> <span class="pre">16)</span> <span class="pre">==</span>
|
||
<span class="pre">324</span></code>. If the base is specified as 0, the number is interpreted using Python’s
|
||
rules: a leading ‘0o’ indicates octal, and ‘0x’ indicates a hex number.</p>
|
||
<p>Do not use the built-in function <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> if all you need is to convert
|
||
strings to numbers. <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> will be significantly slower and it presents a
|
||
security risk: someone could pass you a Python expression that might have
|
||
unwanted side effects. For example, someone could pass
|
||
<code class="docutils literal notranslate"><span class="pre">__import__('os').system("rm</span> <span class="pre">-rf</span> <span class="pre">$HOME")</span></code> which would erase your home
|
||
directory.</p>
|
||
<p><a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> also has the effect of interpreting numbers as Python expressions,
|
||
so that e.g. <code class="docutils literal notranslate"><span class="pre">eval('09')</span></code> gives a syntax error because Python does not allow
|
||
leading ‘0’ in a decimal number (except ‘0’).</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-convert-a-number-to-a-string">
|
||
<h3><a class="toc-backref" href="#id30">How do I convert a number to a string?</a><a class="headerlink" href="#how-do-i-convert-a-number-to-a-string" title="Permalink to this headline">¶</a></h3>
|
||
<p>To convert, e.g., the number 144 to the string ‘144’, use the built-in type
|
||
constructor <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>. If you want a hexadecimal or octal representation, use
|
||
the built-in functions <a class="reference internal" href="../library/functions.html#hex" title="hex"><code class="xref py py-func docutils literal notranslate"><span class="pre">hex()</span></code></a> or <a class="reference internal" href="../library/functions.html#oct" title="oct"><code class="xref py py-func docutils literal notranslate"><span class="pre">oct()</span></code></a>. For fancy formatting, see
|
||
the <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">Formatted string literals</span></a> and <a class="reference internal" href="../library/string.html#formatstrings"><span class="std std-ref">Format String Syntax</span></a> sections,
|
||
e.g. <code class="docutils literal notranslate"><span class="pre">"{:04d}".format(144)</span></code> yields
|
||
<code class="docutils literal notranslate"><span class="pre">'0144'</span></code> and <code class="docutils literal notranslate"><span class="pre">"{:.3f}".format(1.0/3.0)</span></code> yields <code class="docutils literal notranslate"><span class="pre">'0.333'</span></code>.</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-modify-a-string-in-place">
|
||
<h3><a class="toc-backref" href="#id31">How do I modify a string in place?</a><a class="headerlink" href="#how-do-i-modify-a-string-in-place" title="Permalink to this headline">¶</a></h3>
|
||
<p>You can’t, because strings are immutable. In most situations, you should
|
||
simply construct a new string from the various parts you want to assemble
|
||
it from. However, if you need an object with the ability to modify in-place
|
||
unicode data, try using an <a class="reference internal" href="../library/io.html#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.StringIO</span></code></a> object or the <a class="reference internal" href="../library/array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a>
|
||
module:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">io</span>
|
||
<span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="s2">"Hello, world"</span>
|
||
<span class="gp">>>> </span><span class="n">sio</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">StringIO</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">sio</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
|
||
<span class="go">'Hello, world'</span>
|
||
<span class="gp">>>> </span><span class="n">sio</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
|
||
<span class="go">7</span>
|
||
<span class="gp">>>> </span><span class="n">sio</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"there!"</span><span class="p">)</span>
|
||
<span class="go">6</span>
|
||
<span class="gp">>>> </span><span class="n">sio</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
|
||
<span class="go">'Hello, there!'</span>
|
||
|
||
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">array</span>
|
||
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">array</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="s1">'u'</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
|
||
<span class="go">array('u', 'Hello, world')</span>
|
||
<span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'y'</span>
|
||
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
|
||
<span class="go">array('u', 'yello, world')</span>
|
||
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">tounicode</span><span class="p">()</span>
|
||
<span class="go">'yello, world'</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="how-do-i-use-strings-to-call-functions-methods">
|
||
<h3><a class="toc-backref" href="#id32">How do I use strings to call functions/methods?</a><a class="headerlink" href="#how-do-i-use-strings-to-call-functions-methods" title="Permalink to this headline">¶</a></h3>
|
||
<p>There are various techniques.</p>
|
||
<ul>
|
||
<li><p>The best is to use a dictionary that maps strings to functions. The primary
|
||
advantage of this technique is that the strings do not need to match the names
|
||
of the functions. This is also the primary technique used to emulate a case
|
||
construct:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">a</span><span class="p">():</span>
|
||
<span class="k">pass</span>
|
||
|
||
<span class="k">def</span> <span class="nf">b</span><span class="p">():</span>
|
||
<span class="k">pass</span>
|
||
|
||
<span class="n">dispatch</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'go'</span><span class="p">:</span> <span class="n">a</span><span class="p">,</span> <span class="s1">'stop'</span><span class="p">:</span> <span class="n">b</span><span class="p">}</span> <span class="c1"># Note lack of parens for funcs</span>
|
||
|
||
<span class="n">dispatch</span><span class="p">[</span><span class="n">get_input</span><span class="p">()]()</span> <span class="c1"># Note trailing parens to call function</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
<li><p>Use the built-in function <a class="reference internal" href="../library/functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">foo</span>
|
||
<span class="nb">getattr</span><span class="p">(</span><span class="n">foo</span><span class="p">,</span> <span class="s1">'bar'</span><span class="p">)()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Note that <a class="reference internal" href="../library/functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> works on any object, including classes, class
|
||
instances, modules, and so on.</p>
|
||
<p>This is used in several places in the standard library, like this:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Foo</span><span class="p">:</span>
|
||
<span class="k">def</span> <span class="nf">do_foo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="o">...</span>
|
||
|
||
<span class="k">def</span> <span class="nf">do_bar</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="o">...</span>
|
||
|
||
<span class="n">f</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">foo_instance</span><span class="p">,</span> <span class="s1">'do_'</span> <span class="o">+</span> <span class="n">opname</span><span class="p">)</span>
|
||
<span class="n">f</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
<li><p>Use <a class="reference internal" href="../library/functions.html#locals" title="locals"><code class="xref py py-func docutils literal notranslate"><span class="pre">locals()</span></code></a> or <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> to resolve the function name:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">myFunc</span><span class="p">():</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">)</span>
|
||
|
||
<span class="n">fname</span> <span class="o">=</span> <span class="s2">"myFunc"</span>
|
||
|
||
<span class="n">f</span> <span class="o">=</span> <span class="nb">locals</span><span class="p">()[</span><span class="n">fname</span><span class="p">]</span>
|
||
<span class="n">f</span><span class="p">()</span>
|
||
|
||
<span class="n">f</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">fname</span><span class="p">)</span>
|
||
<span class="n">f</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Note: Using <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> is slow and dangerous. If you don’t have absolute
|
||
control over the contents of the string, someone could pass a string that
|
||
resulted in an arbitrary function being executed.</p>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="is-there-an-equivalent-to-perl-s-chomp-for-removing-trailing-newlines-from-strings">
|
||
<h3><a class="toc-backref" href="#id33">Is there an equivalent to Perl’s chomp() for removing trailing newlines from strings?</a><a class="headerlink" href="#is-there-an-equivalent-to-perl-s-chomp-for-removing-trailing-newlines-from-strings" title="Permalink to this headline">¶</a></h3>
|
||
<p>You can use <code class="docutils literal notranslate"><span class="pre">S.rstrip("\r\n")</span></code> to remove all occurrences of any line
|
||
terminator from the end of the string <code class="docutils literal notranslate"><span class="pre">S</span></code> without removing other trailing
|
||
whitespace. If the string <code class="docutils literal notranslate"><span class="pre">S</span></code> represents more than one line, with several
|
||
empty lines at the end, the line terminators for all the blank lines will
|
||
be removed:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">lines</span> <span class="o">=</span> <span class="p">(</span><span class="s2">"line 1 </span><span class="se">\r\n</span><span class="s2">"</span>
|
||
<span class="gp">... </span> <span class="s2">"</span><span class="se">\r\n</span><span class="s2">"</span>
|
||
<span class="gp">... </span> <span class="s2">"</span><span class="se">\r\n</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">lines</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s2">"</span><span class="se">\n\r</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="go">'line 1 '</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Since this is typically only desired when reading text one line at a time, using
|
||
<code class="docutils literal notranslate"><span class="pre">S.rstrip()</span></code> this way works well.</p>
|
||
</div>
|
||
<div class="section" id="is-there-a-scanf-or-sscanf-equivalent">
|
||
<h3><a class="toc-backref" href="#id34">Is there a scanf() or sscanf() equivalent?</a><a class="headerlink" href="#is-there-a-scanf-or-sscanf-equivalent" title="Permalink to this headline">¶</a></h3>
|
||
<p>Not as such.</p>
|
||
<p>For simple input parsing, the easiest approach is usually to split the line into
|
||
whitespace-delimited words using the <a class="reference internal" href="../library/stdtypes.html#str.split" title="str.split"><code class="xref py py-meth docutils literal notranslate"><span class="pre">split()</span></code></a> method of string objects
|
||
and then convert decimal strings to numeric values using <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> or
|
||
<a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a>. <code class="docutils literal notranslate"><span class="pre">split()</span></code> supports an optional “sep” parameter which is useful
|
||
if the line uses something other than whitespace as a separator.</p>
|
||
<p>For more complicated input parsing, regular expressions are more powerful
|
||
than C’s <code class="xref c c-func docutils literal notranslate"><span class="pre">sscanf()</span></code> and better suited for the task.</p>
|
||
</div>
|
||
<div class="section" id="what-does-unicodedecodeerror-or-unicodeencodeerror-error-mean">
|
||
<h3><a class="toc-backref" href="#id35">What does ‘UnicodeDecodeError’ or ‘UnicodeEncodeError’ error mean?</a><a class="headerlink" href="#what-does-unicodedecodeerror-or-unicodeencodeerror-error-mean" title="Permalink to this headline">¶</a></h3>
|
||
<p>See the <a class="reference internal" href="../howto/unicode.html#unicode-howto"><span class="std std-ref">Unicode HOWTO</span></a>.</p>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="performance">
|
||
<h2><a class="toc-backref" href="#id36">Performance</a><a class="headerlink" href="#performance" title="Permalink to this headline">¶</a></h2>
|
||
<div class="section" id="my-program-is-too-slow-how-do-i-speed-it-up">
|
||
<h3><a class="toc-backref" href="#id37">My program is too slow. How do I speed it up?</a><a class="headerlink" href="#my-program-is-too-slow-how-do-i-speed-it-up" title="Permalink to this headline">¶</a></h3>
|
||
<p>That’s a tough one, in general. First, here are a list of things to
|
||
remember before diving further:</p>
|
||
<ul class="simple">
|
||
<li><p>Performance characteristics vary across Python implementations. This FAQ
|
||
focusses on <a class="reference internal" href="../glossary.html#term-cpython"><span class="xref std std-term">CPython</span></a>.</p></li>
|
||
<li><p>Behaviour can vary across operating systems, especially when talking about
|
||
I/O or multi-threading.</p></li>
|
||
<li><p>You should always find the hot spots in your program <em>before</em> attempting to
|
||
optimize any code (see the <a class="reference internal" href="../library/profile.html#module-profile" title="profile: Python source profiler."><code class="xref py py-mod docutils literal notranslate"><span class="pre">profile</span></code></a> module).</p></li>
|
||
<li><p>Writing benchmark scripts will allow you to iterate quickly when searching
|
||
for improvements (see the <a class="reference internal" href="../library/timeit.html#module-timeit" title="timeit: Measure the execution time of small code snippets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeit</span></code></a> module).</p></li>
|
||
<li><p>It is highly recommended to have good code coverage (through unit testing
|
||
or any other technique) before potentially introducing regressions hidden
|
||
in sophisticated optimizations.</p></li>
|
||
</ul>
|
||
<p>That being said, there are many tricks to speed up Python code. Here are
|
||
some general principles which go a long way towards reaching acceptable
|
||
performance levels:</p>
|
||
<ul class="simple">
|
||
<li><p>Making your algorithms faster (or changing to faster ones) can yield
|
||
much larger benefits than trying to sprinkle micro-optimization tricks
|
||
all over your code.</p></li>
|
||
<li><p>Use the right data structures. Study documentation for the <a class="reference internal" href="../library/stdtypes.html#bltin-types"><span class="std std-ref">Built-in Types</span></a>
|
||
and the <a class="reference internal" href="../library/collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module.</p></li>
|
||
<li><p>When the standard library provides a primitive for doing something, it is
|
||
likely (although not guaranteed) to be faster than any alternative you
|
||
may come up with. This is doubly true for primitives written in C, such
|
||
as builtins and some extension types. For example, be sure to use
|
||
either the <a class="reference internal" href="../library/stdtypes.html#list.sort" title="list.sort"><code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code></a> built-in method or the related <a class="reference internal" href="../library/functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a>
|
||
function to do sorting (and see the <a class="reference internal" href="../howto/sorting.html#sortinghowto"><span class="std std-ref">Sorting HOW TO</span></a> for examples
|
||
of moderately advanced usage).</p></li>
|
||
<li><p>Abstractions tend to create indirections and force the interpreter to work
|
||
more. If the levels of indirection outweigh the amount of useful work
|
||
done, your program will be slower. You should avoid excessive abstraction,
|
||
especially under the form of tiny functions or methods (which are also often
|
||
detrimental to readability).</p></li>
|
||
</ul>
|
||
<p>If you have reached the limit of what pure Python can allow, there are tools
|
||
to take you further away. For example, <a class="reference external" href="http://cython.org">Cython</a> can
|
||
compile a slightly modified version of Python code into a C extension, and
|
||
can be used on many different platforms. Cython can take advantage of
|
||
compilation (and optional type annotations) to make your code significantly
|
||
faster than when interpreted. If you are confident in your C programming
|
||
skills, you can also <a class="reference internal" href="../extending/index.html#extending-index"><span class="std std-ref">write a C extension module</span></a>
|
||
yourself.</p>
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">See also</p>
|
||
<p>The wiki page devoted to <a class="reference external" href="https://wiki.python.org/moin/PythonSpeed/PerformanceTips">performance tips</a>.</p>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="what-is-the-most-efficient-way-to-concatenate-many-strings-together">
|
||
<span id="efficient-string-concatenation"></span><h3><a class="toc-backref" href="#id38">What is the most efficient way to concatenate many strings together?</a><a class="headerlink" href="#what-is-the-most-efficient-way-to-concatenate-many-strings-together" title="Permalink to this headline">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/stdtypes.html#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="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> objects are immutable, therefore concatenating
|
||
many strings together is inefficient as each concatenation creates a new
|
||
object. In the general case, the total runtime cost is quadratic in the
|
||
total string length.</p>
|
||
<p>To accumulate many <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> objects, the recommended idiom is to place
|
||
them into a list and call <a class="reference internal" href="../library/stdtypes.html#str.join" title="str.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.join()</span></code></a> at the end:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">chunks</span> <span class="o">=</span> <span class="p">[]</span>
|
||
<span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">my_strings</span><span class="p">:</span>
|
||
<span class="n">chunks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
|
||
<span class="n">result</span> <span class="o">=</span> <span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">chunks</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(another reasonably efficient idiom is to use <a class="reference internal" href="../library/io.html#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.StringIO</span></code></a>)</p>
|
||
<p>To accumulate many <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> objects, the recommended idiom is to extend
|
||
a <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> object using in-place concatenation (the <code class="docutils literal notranslate"><span class="pre">+=</span></code> operator):</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">result</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">()</span>
|
||
<span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">my_bytes_objects</span><span class="p">:</span>
|
||
<span class="n">result</span> <span class="o">+=</span> <span class="n">b</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="sequences-tuples-lists">
|
||
<h2><a class="toc-backref" href="#id39">Sequences (Tuples/Lists)</a><a class="headerlink" href="#sequences-tuples-lists" title="Permalink to this headline">¶</a></h2>
|
||
<div class="section" id="how-do-i-convert-between-tuples-and-lists">
|
||
<h3><a class="toc-backref" href="#id40">How do I convert between tuples and lists?</a><a class="headerlink" href="#how-do-i-convert-between-tuples-and-lists" title="Permalink to this headline">¶</a></h3>
|
||
<p>The type constructor <code class="docutils literal notranslate"><span class="pre">tuple(seq)</span></code> converts any sequence (actually, any
|
||
iterable) into a tuple with the same items in the same order.</p>
|
||
<p>For example, <code class="docutils literal notranslate"><span class="pre">tuple([1,</span> <span class="pre">2,</span> <span class="pre">3])</span></code> yields <code class="docutils literal notranslate"><span class="pre">(1,</span> <span class="pre">2,</span> <span class="pre">3)</span></code> and <code class="docutils literal notranslate"><span class="pre">tuple('abc')</span></code>
|
||
yields <code class="docutils literal notranslate"><span class="pre">('a',</span> <span class="pre">'b',</span> <span class="pre">'c')</span></code>. If the argument is a tuple, it does not make a copy
|
||
but returns the same object, so it is cheap to call <a class="reference internal" href="../library/stdtypes.html#tuple" title="tuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">tuple()</span></code></a> when you
|
||
aren’t sure that an object is already a tuple.</p>
|
||
<p>The type constructor <code class="docutils literal notranslate"><span class="pre">list(seq)</span></code> converts any sequence or iterable into a list
|
||
with the same items in the same order. For example, <code class="docutils literal notranslate"><span class="pre">list((1,</span> <span class="pre">2,</span> <span class="pre">3))</span></code> yields
|
||
<code class="docutils literal notranslate"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></code> and <code class="docutils literal notranslate"><span class="pre">list('abc')</span></code> yields <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></code>. If the argument
|
||
is a list, it makes a copy just like <code class="docutils literal notranslate"><span class="pre">seq[:]</span></code> would.</p>
|
||
</div>
|
||
<div class="section" id="what-s-a-negative-index">
|
||
<h3><a class="toc-backref" href="#id41">What’s a negative index?</a><a class="headerlink" href="#what-s-a-negative-index" title="Permalink to this headline">¶</a></h3>
|
||
<p>Python sequences are indexed with positive numbers and negative numbers. For
|
||
positive numbers 0 is the first index 1 is the second index and so forth. For
|
||
negative indices -1 is the last index and -2 is the penultimate (next to last)
|
||
index and so forth. Think of <code class="docutils literal notranslate"><span class="pre">seq[-n]</span></code> as the same as <code class="docutils literal notranslate"><span class="pre">seq[len(seq)-n]</span></code>.</p>
|
||
<p>Using negative indices can be very convenient. For example <code class="docutils literal notranslate"><span class="pre">S[:-1]</span></code> is all of
|
||
the string except for its last character, which is useful for removing the
|
||
trailing newline from a string.</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-iterate-over-a-sequence-in-reverse-order">
|
||
<h3><a class="toc-backref" href="#id42">How do I iterate over a sequence in reverse order?</a><a class="headerlink" href="#how-do-i-iterate-over-a-sequence-in-reverse-order" title="Permalink to this headline">¶</a></h3>
|
||
<p>Use the <a class="reference internal" href="../library/functions.html#reversed" title="reversed"><code class="xref py py-func docutils literal notranslate"><span class="pre">reversed()</span></code></a> built-in function, which is new in Python 2.4:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">sequence</span><span class="p">):</span>
|
||
<span class="o">...</span> <span class="c1"># do something with x ...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This won’t touch your original sequence, but build a new copy with reversed
|
||
order to iterate over.</p>
|
||
<p>With Python 2.3, you can use an extended slice syntax:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">sequence</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
|
||
<span class="o">...</span> <span class="c1"># do something with x ...</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="how-do-you-remove-duplicates-from-a-list">
|
||
<h3><a class="toc-backref" href="#id43">How do you remove duplicates from a list?</a><a class="headerlink" href="#how-do-you-remove-duplicates-from-a-list" title="Permalink to this headline">¶</a></h3>
|
||
<p>See the Python Cookbook for a long discussion of many ways to do this:</p>
|
||
<blockquote>
|
||
<div><p><a class="reference external" href="https://code.activestate.com/recipes/52560/">https://code.activestate.com/recipes/52560/</a></p>
|
||
</div></blockquote>
|
||
<p>If you don’t mind reordering the list, sort it and then scan from the end of the
|
||
list, deleting duplicates as you go:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">mylist</span><span class="p">:</span>
|
||
<span class="n">mylist</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
|
||
<span class="n">last</span> <span class="o">=</span> <span class="n">mylist</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
|
||
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">mylist</span><span class="p">)</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">):</span>
|
||
<span class="k">if</span> <span class="n">last</span> <span class="o">==</span> <span class="n">mylist</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
|
||
<span class="k">del</span> <span class="n">mylist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
|
||
<span class="k">else</span><span class="p">:</span>
|
||
<span class="n">last</span> <span class="o">=</span> <span class="n">mylist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If all elements of the list may be used as set keys (i.e. they are all
|
||
<a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>) this is often faster</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">mylist</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">mylist</span><span class="p">))</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This converts the list into a set, thereby removing duplicates, and then back
|
||
into a list.</p>
|
||
</div>
|
||
<div class="section" id="how-do-you-make-an-array-in-python">
|
||
<h3><a class="toc-backref" href="#id44">How do you make an array in Python?</a><a class="headerlink" href="#how-do-you-make-an-array-in-python" title="Permalink to this headline">¶</a></h3>
|
||
<p>Use a list:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="s2">"this"</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="s2">"is"</span><span class="p">,</span> <span class="s2">"an"</span><span class="p">,</span> <span class="s2">"array"</span><span class="p">]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Lists are equivalent to C or Pascal arrays in their time complexity; the primary
|
||
difference is that a Python list can contain objects of many different types.</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">array</span></code> module also provides methods for creating arrays of fixed types
|
||
with compact representations, but they are slower to index than lists. Also
|
||
note that the Numeric extensions and others define array-like structures with
|
||
various characteristics as well.</p>
|
||
<p>To get Lisp-style linked lists, you can emulate cons cells using tuples:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">lisp_list</span> <span class="o">=</span> <span class="p">(</span><span class="s2">"like"</span><span class="p">,</span> <span class="p">(</span><span class="s2">"this"</span><span class="p">,</span> <span class="p">(</span><span class="s2">"example"</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="p">)</span> <span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If mutability is desired, you could use lists instead of tuples. Here the
|
||
analogue of lisp car is <code class="docutils literal notranslate"><span class="pre">lisp_list[0]</span></code> and the analogue of cdr is
|
||
<code class="docutils literal notranslate"><span class="pre">lisp_list[1]</span></code>. Only do this if you’re sure you really need to, because it’s
|
||
usually a lot slower than using Python lists.</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-create-a-multidimensional-list">
|
||
<span id="faq-multidimensional-list"></span><h3><a class="toc-backref" href="#id45">How do I create a multidimensional list?</a><a class="headerlink" href="#how-do-i-create-a-multidimensional-list" title="Permalink to this headline">¶</a></h3>
|
||
<p>You probably tried to make a multidimensional array like this:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">A</span> <span class="o">=</span> <span class="p">[[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This looks correct if you print it:</p>
|
||
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">A</span>
|
||
<span class="go">[[None, None], [None, None], [None, None]]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>But when you assign a value, it shows up in multiple places:</p>
|
||
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">A</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">5</span>
|
||
<span class="gp">>>> </span><span class="n">A</span>
|
||
<span class="go">[[5, None], [5, None], [5, None]]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The reason is that replicating a list with <code class="docutils literal notranslate"><span class="pre">*</span></code> doesn’t create copies, it only
|
||
creates references to the existing objects. The <code class="docutils literal notranslate"><span class="pre">*3</span></code> creates a list
|
||
containing 3 references to the same list of length two. Changes to one row will
|
||
show in all rows, which is almost certainly not what you want.</p>
|
||
<p>The suggested approach is to create a list of the desired length first and then
|
||
fill in each element with a newly created list:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">A</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span>
|
||
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
|
||
<span class="n">A</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This generates a list containing 3 different lists of length two. You can also
|
||
use a list comprehension:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">w</span><span class="p">,</span> <span class="n">h</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span>
|
||
<span class="n">A</span> <span class="o">=</span> <span class="p">[[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="n">w</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">h</span><span class="p">)]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Or, you can use an extension that provides a matrix datatype; <a class="reference external" href="http://www.numpy.org/">NumPy</a> is the best known.</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-apply-a-method-to-a-sequence-of-objects">
|
||
<h3><a class="toc-backref" href="#id46">How do I apply a method to a sequence of objects?</a><a class="headerlink" href="#how-do-i-apply-a-method-to-a-sequence-of-objects" title="Permalink to this headline">¶</a></h3>
|
||
<p>Use a list comprehension:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">result</span> <span class="o">=</span> <span class="p">[</span><span class="n">obj</span><span class="o">.</span><span class="n">method</span><span class="p">()</span> <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">mylist</span><span class="p">]</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="why-does-a-tuple-i-item-raise-an-exception-when-the-addition-works">
|
||
<span id="faq-augmented-assignment-tuple-error"></span><h3><a class="toc-backref" href="#id47">Why does a_tuple[i] += [‘item’] raise an exception when the addition works?</a><a class="headerlink" href="#why-does-a-tuple-i-item-raise-an-exception-when-the-addition-works" title="Permalink to this headline">¶</a></h3>
|
||
<p>This is because of a combination of the fact that augmented assignment
|
||
operators are <em>assignment</em> operators, and the difference between mutable and
|
||
immutable objects in Python.</p>
|
||
<p>This discussion applies in general when augmented assignment operators are
|
||
applied to elements of a tuple that point to mutable objects, but we’ll use
|
||
a <code class="docutils literal notranslate"><span class="pre">list</span></code> and <code class="docutils literal notranslate"><span class="pre">+=</span></code> as our exemplar.</p>
|
||
<p>If you wrote:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a_tuple</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="gp">>>> </span><span class="n">a_tuple</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
<span class="c">...</span>
|
||
<span class="gr">TypeError</span>: <span class="n">'tuple' object does not support item assignment</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The reason for the exception should be immediately clear: <code class="docutils literal notranslate"><span class="pre">1</span></code> is added to the
|
||
object <code class="docutils literal notranslate"><span class="pre">a_tuple[0]</span></code> points to (<code class="docutils literal notranslate"><span class="pre">1</span></code>), producing the result object, <code class="docutils literal notranslate"><span class="pre">2</span></code>,
|
||
but when we attempt to assign the result of the computation, <code class="docutils literal notranslate"><span class="pre">2</span></code>, to element
|
||
<code class="docutils literal notranslate"><span class="pre">0</span></code> of the tuple, we get an error because we can’t change what an element of
|
||
a tuple points to.</p>
|
||
<p>Under the covers, what this augmented assignment statement is doing is
|
||
approximately this:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">a_tuple</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
|
||
<span class="gp">>>> </span><span class="n">a_tuple</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
<span class="c">...</span>
|
||
<span class="gr">TypeError</span>: <span class="n">'tuple' object does not support item assignment</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>It is the assignment part of the operation that produces the error, since a
|
||
tuple is immutable.</p>
|
||
<p>When you write something like:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a_tuple</span> <span class="o">=</span> <span class="p">([</span><span class="s1">'foo'</span><span class="p">],</span> <span class="s1">'bar'</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">a_tuple</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+=</span> <span class="p">[</span><span class="s1">'item'</span><span class="p">]</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
<span class="c">...</span>
|
||
<span class="gr">TypeError</span>: <span class="n">'tuple' object does not support item assignment</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The exception is a bit more surprising, and even more surprising is the fact
|
||
that even though there was an error, the append worked:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a_tuple</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
||
<span class="go">['foo', 'item']</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>To see why this happens, you need to know that (a) if an object implements an
|
||
<code class="docutils literal notranslate"><span class="pre">__iadd__</span></code> magic method, it gets called when the <code class="docutils literal notranslate"><span class="pre">+=</span></code> augmented assignment
|
||
is executed, and its return value is what gets used in the assignment statement;
|
||
and (b) for lists, <code class="docutils literal notranslate"><span class="pre">__iadd__</span></code> is equivalent to calling <code class="docutils literal notranslate"><span class="pre">extend</span></code> on the list
|
||
and returning the list. That’s why we say that for lists, <code class="docutils literal notranslate"><span class="pre">+=</span></code> is a
|
||
“shorthand” for <code class="docutils literal notranslate"><span class="pre">list.extend</span></code>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a_list</span> <span class="o">=</span> <span class="p">[]</span>
|
||
<span class="gp">>>> </span><span class="n">a_list</span> <span class="o">+=</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
||
<span class="gp">>>> </span><span class="n">a_list</span>
|
||
<span class="go">[1]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This is equivalent to:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">a_list</span><span class="o">.</span><span class="fm">__iadd__</span><span class="p">([</span><span class="mi">1</span><span class="p">])</span>
|
||
<span class="gp">>>> </span><span class="n">a_list</span> <span class="o">=</span> <span class="n">result</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The object pointed to by a_list has been mutated, and the pointer to the
|
||
mutated object is assigned back to <code class="docutils literal notranslate"><span class="pre">a_list</span></code>. The end result of the
|
||
assignment is a no-op, since it is a pointer to the same object that <code class="docutils literal notranslate"><span class="pre">a_list</span></code>
|
||
was previously pointing to, but the assignment still happens.</p>
|
||
<p>Thus, in our tuple example what is happening is equivalent to:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">a_tuple</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="fm">__iadd__</span><span class="p">([</span><span class="s1">'item'</span><span class="p">])</span>
|
||
<span class="gp">>>> </span><span class="n">a_tuple</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
<span class="c">...</span>
|
||
<span class="gr">TypeError</span>: <span class="n">'tuple' object does not support item assignment</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">__iadd__</span></code> succeeds, and thus the list is extended, but even though
|
||
<code class="docutils literal notranslate"><span class="pre">result</span></code> points to the same object that <code class="docutils literal notranslate"><span class="pre">a_tuple[0]</span></code> already points to,
|
||
that final assignment still results in an error, because tuples are immutable.</p>
|
||
</div>
|
||
<div class="section" id="i-want-to-do-a-complicated-sort-can-you-do-a-schwartzian-transform-in-python">
|
||
<h3><a class="toc-backref" href="#id48">I want to do a complicated sort: can you do a Schwartzian Transform in Python?</a><a class="headerlink" href="#i-want-to-do-a-complicated-sort-can-you-do-a-schwartzian-transform-in-python" title="Permalink to this headline">¶</a></h3>
|
||
<p>The technique, attributed to Randal Schwartz of the Perl community, sorts the
|
||
elements of a list by a metric which maps each element to its “sort value”. In
|
||
Python, use the <code class="docutils literal notranslate"><span class="pre">key</span></code> argument for the <a class="reference internal" href="../library/stdtypes.html#list.sort" title="list.sort"><code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code></a> method:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Isorted</span> <span class="o">=</span> <span class="n">L</span><span class="p">[:]</span>
|
||
<span class="n">Isorted</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="mi">10</span><span class="p">:</span><span class="mi">15</span><span class="p">]))</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="how-can-i-sort-one-list-by-values-from-another-list">
|
||
<h3><a class="toc-backref" href="#id49">How can I sort one list by values from another list?</a><a class="headerlink" href="#how-can-i-sort-one-list-by-values-from-another-list" title="Permalink to this headline">¶</a></h3>
|
||
<p>Merge them into an iterator of tuples, sort the resulting list, and then pick
|
||
out the element you want.</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">list1</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"what"</span><span class="p">,</span> <span class="s2">"I'm"</span><span class="p">,</span> <span class="s2">"sorting"</span><span class="p">,</span> <span class="s2">"by"</span><span class="p">]</span>
|
||
<span class="gp">>>> </span><span class="n">list2</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"something"</span><span class="p">,</span> <span class="s2">"else"</span><span class="p">,</span> <span class="s2">"to"</span><span class="p">,</span> <span class="s2">"sort"</span><span class="p">]</span>
|
||
<span class="gp">>>> </span><span class="n">pairs</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="n">list1</span><span class="p">,</span> <span class="n">list2</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">pairs</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">pairs</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">pairs</span>
|
||
<span class="go">[("I'm", 'else'), ('by', 'sort'), ('sorting', 'to'), ('what', 'something')]</span>
|
||
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">pairs</span><span class="p">]</span>
|
||
<span class="gp">>>> </span><span class="n">result</span>
|
||
<span class="go">['else', 'sort', 'to', 'something']</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>An alternative for the last step is:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
|
||
<span class="gp">>>> </span><span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">pairs</span><span class="p">:</span> <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If you find this more legible, you might prefer to use this instead of the final
|
||
list comprehension. However, it is almost twice as slow for long lists. Why?
|
||
First, the <code class="docutils literal notranslate"><span class="pre">append()</span></code> operation has to reallocate memory, and while it uses
|
||
some tricks to avoid doing that each time, it still has to do it occasionally,
|
||
and that costs quite a bit. Second, the expression “result.append” requires an
|
||
extra attribute lookup, and third, there’s a speed reduction from having to make
|
||
all those function calls.</p>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="objects">
|
||
<h2><a class="toc-backref" href="#id50">Objects</a><a class="headerlink" href="#objects" title="Permalink to this headline">¶</a></h2>
|
||
<div class="section" id="what-is-a-class">
|
||
<h3><a class="toc-backref" href="#id51">What is a class?</a><a class="headerlink" href="#what-is-a-class" title="Permalink to this headline">¶</a></h3>
|
||
<p>A class is the particular object type created by executing a class statement.
|
||
Class objects are used as templates to create instance objects, which embody
|
||
both the data (attributes) and code (methods) specific to a datatype.</p>
|
||
<p>A class can be based on one or more other classes, called its base class(es). It
|
||
then inherits the attributes and methods of its base classes. This allows an
|
||
object model to be successively refined by inheritance. You might have a
|
||
generic <code class="docutils literal notranslate"><span class="pre">Mailbox</span></code> class that provides basic accessor methods for a mailbox,
|
||
and subclasses such as <code class="docutils literal notranslate"><span class="pre">MboxMailbox</span></code>, <code class="docutils literal notranslate"><span class="pre">MaildirMailbox</span></code>, <code class="docutils literal notranslate"><span class="pre">OutlookMailbox</span></code>
|
||
that handle various specific mailbox formats.</p>
|
||
</div>
|
||
<div class="section" id="what-is-a-method">
|
||
<h3><a class="toc-backref" href="#id52">What is a method?</a><a class="headerlink" href="#what-is-a-method" title="Permalink to this headline">¶</a></h3>
|
||
<p>A method is a function on some object <code class="docutils literal notranslate"><span class="pre">x</span></code> that you normally call as
|
||
<code class="docutils literal notranslate"><span class="pre">x.name(arguments...)</span></code>. Methods are defined as functions inside the class
|
||
definition:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
|
||
<span class="k">def</span> <span class="nf">meth</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="k">return</span> <span class="n">arg</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">attribute</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="what-is-self">
|
||
<h3><a class="toc-backref" href="#id53">What is self?</a><a class="headerlink" href="#what-is-self" title="Permalink to this headline">¶</a></h3>
|
||
<p>Self is merely a conventional name for the first argument of a method. A method
|
||
defined as <code class="docutils literal notranslate"><span class="pre">meth(self,</span> <span class="pre">a,</span> <span class="pre">b,</span> <span class="pre">c)</span></code> should be called as <code class="docutils literal notranslate"><span class="pre">x.meth(a,</span> <span class="pre">b,</span> <span class="pre">c)</span></code> for
|
||
some instance <code class="docutils literal notranslate"><span class="pre">x</span></code> of the class in which the definition occurs; the called
|
||
method will think it is called as <code class="docutils literal notranslate"><span class="pre">meth(x,</span> <span class="pre">a,</span> <span class="pre">b,</span> <span class="pre">c)</span></code>.</p>
|
||
<p>See also <a class="reference internal" href="design.html#why-self"><span class="std std-ref">Why must ‘self’ be used explicitly in method definitions and calls?</span></a>.</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-check-if-an-object-is-an-instance-of-a-given-class-or-of-a-subclass-of-it">
|
||
<h3><a class="toc-backref" href="#id54">How do I check if an object is an instance of a given class or of a subclass of it?</a><a class="headerlink" href="#how-do-i-check-if-an-object-is-an-instance-of-a-given-class-or-of-a-subclass-of-it" title="Permalink to this headline">¶</a></h3>
|
||
<p>Use the built-in function <code class="docutils literal notranslate"><span class="pre">isinstance(obj,</span> <span class="pre">cls)</span></code>. You can check if an object
|
||
is an instance of any of a number of classes by providing a tuple instead of a
|
||
single class, e.g. <code class="docutils literal notranslate"><span class="pre">isinstance(obj,</span> <span class="pre">(class1,</span> <span class="pre">class2,</span> <span class="pre">...))</span></code>, and can also
|
||
check whether an object is one of Python’s built-in types, e.g.
|
||
<code class="docutils literal notranslate"><span class="pre">isinstance(obj,</span> <span class="pre">str)</span></code> or <code class="docutils literal notranslate"><span class="pre">isinstance(obj,</span> <span class="pre">(int,</span> <span class="pre">float,</span> <span class="pre">complex))</span></code>.</p>
|
||
<p>Note that most programs do not use <a class="reference internal" href="../library/functions.html#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a> on user-defined classes
|
||
very often. If you are developing the classes yourself, a more proper
|
||
object-oriented style is to define methods on the classes that encapsulate a
|
||
particular behaviour, instead of checking the object’s class and doing a
|
||
different thing based on what class it is. For example, if you have a function
|
||
that does something:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">search</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
|
||
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">Mailbox</span><span class="p">):</span>
|
||
<span class="o">...</span> <span class="c1"># code to search a mailbox</span>
|
||
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">Document</span><span class="p">):</span>
|
||
<span class="o">...</span> <span class="c1"># code to search a document</span>
|
||
<span class="k">elif</span> <span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>A better approach is to define a <code class="docutils literal notranslate"><span class="pre">search()</span></code> method on all the classes and just
|
||
call it:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Mailbox</span><span class="p">:</span>
|
||
<span class="k">def</span> <span class="nf">search</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="o">...</span> <span class="c1"># code to search a mailbox</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Document</span><span class="p">:</span>
|
||
<span class="k">def</span> <span class="nf">search</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="o">...</span> <span class="c1"># code to search a document</span>
|
||
|
||
<span class="n">obj</span><span class="o">.</span><span class="n">search</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="what-is-delegation">
|
||
<h3><a class="toc-backref" href="#id55">What is delegation?</a><a class="headerlink" href="#what-is-delegation" title="Permalink to this headline">¶</a></h3>
|
||
<p>Delegation is an object oriented technique (also called a design pattern).
|
||
Let’s say you have an object <code class="docutils literal notranslate"><span class="pre">x</span></code> and want to change the behaviour of just one
|
||
of its methods. You can create a new class that provides a new implementation
|
||
of the method you’re interested in changing and delegates all other methods to
|
||
the corresponding method of <code class="docutils literal notranslate"><span class="pre">x</span></code>.</p>
|
||
<p>Python programmers can easily implement delegation. For example, the following
|
||
class implements a class that behaves like a file but converts all written data
|
||
to uppercase:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">UpperOut</span><span class="p">:</span>
|
||
|
||
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">outfile</span><span class="p">):</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">_outfile</span> <span class="o">=</span> <span class="n">outfile</span>
|
||
|
||
<span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">):</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">_outfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">upper</span><span class="p">())</span>
|
||
|
||
<span class="k">def</span> <span class="nf">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_outfile</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Here the <code class="docutils literal notranslate"><span class="pre">UpperOut</span></code> class redefines the <code class="docutils literal notranslate"><span class="pre">write()</span></code> method to convert the
|
||
argument string to uppercase before calling the underlying
|
||
<code class="docutils literal notranslate"><span class="pre">self.__outfile.write()</span></code> method. All other methods are delegated to the
|
||
underlying <code class="docutils literal notranslate"><span class="pre">self.__outfile</span></code> object. The delegation is accomplished via the
|
||
<code class="docutils literal notranslate"><span class="pre">__getattr__</span></code> method; consult <a class="reference internal" href="../reference/datamodel.html#attribute-access"><span class="std std-ref">the language reference</span></a>
|
||
for more information about controlling attribute access.</p>
|
||
<p>Note that for more general cases delegation can get trickier. When attributes
|
||
must be set as well as retrieved, the class must define a <a class="reference internal" href="../reference/datamodel.html#object.__setattr__" title="object.__setattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setattr__()</span></code></a>
|
||
method too, and it must do so carefully. The basic implementation of
|
||
<a class="reference internal" href="../reference/datamodel.html#object.__setattr__" title="object.__setattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setattr__()</span></code></a> is roughly equivalent to the following:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">X</span><span class="p">:</span>
|
||
<span class="o">...</span>
|
||
<span class="k">def</span> <span class="nf">__setattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Most <a class="reference internal" href="../reference/datamodel.html#object.__setattr__" title="object.__setattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setattr__()</span></code></a> implementations must modify <code class="docutils literal notranslate"><span class="pre">self.__dict__</span></code> to store
|
||
local state for self without causing an infinite recursion.</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-call-a-method-defined-in-a-base-class-from-a-derived-class-that-overrides-it">
|
||
<h3><a class="toc-backref" href="#id56">How do I call a method defined in a base class from a derived class that overrides it?</a><a class="headerlink" href="#how-do-i-call-a-method-defined-in-a-base-class-from-a-derived-class-that-overrides-it" title="Permalink to this headline">¶</a></h3>
|
||
<p>Use the built-in <a class="reference internal" href="../library/functions.html#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a> function:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Derived</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
|
||
<span class="k">def</span> <span class="nf">meth</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="nb">super</span><span class="p">(</span><span class="n">Derived</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">meth</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>For version prior to 3.0, you may be using classic classes: For a class
|
||
definition such as <code class="docutils literal notranslate"><span class="pre">class</span> <span class="pre">Derived(Base):</span> <span class="pre">...</span></code> you can call method <code class="docutils literal notranslate"><span class="pre">meth()</span></code>
|
||
defined in <code class="docutils literal notranslate"><span class="pre">Base</span></code> (or one of <code class="docutils literal notranslate"><span class="pre">Base</span></code>’s base classes) as <code class="docutils literal notranslate"><span class="pre">Base.meth(self,</span>
|
||
<span class="pre">arguments...)</span></code>. Here, <code class="docutils literal notranslate"><span class="pre">Base.meth</span></code> is an unbound method, so you need to
|
||
provide the <code class="docutils literal notranslate"><span class="pre">self</span></code> argument.</p>
|
||
</div>
|
||
<div class="section" id="how-can-i-organize-my-code-to-make-it-easier-to-change-the-base-class">
|
||
<h3><a class="toc-backref" href="#id57">How can I organize my code to make it easier to change the base class?</a><a class="headerlink" href="#how-can-i-organize-my-code-to-make-it-easier-to-change-the-base-class" title="Permalink to this headline">¶</a></h3>
|
||
<p>You could define an alias for the base class, assign the real base class to it
|
||
before your class definition, and use the alias throughout your class. Then all
|
||
you have to change is the value assigned to the alias. Incidentally, this trick
|
||
is also handy if you want to decide dynamically (e.g. depending on availability
|
||
of resources) which base class to use. Example:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">BaseAlias</span> <span class="o">=</span> <span class="o"><</span><span class="n">real</span> <span class="n">base</span> <span class="n">class</span><span class="o">></span>
|
||
|
||
<span class="k">class</span> <span class="nc">Derived</span><span class="p">(</span><span class="n">BaseAlias</span><span class="p">):</span>
|
||
<span class="k">def</span> <span class="nf">meth</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="n">BaseAlias</span><span class="o">.</span><span class="n">meth</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="how-do-i-create-static-class-data-and-static-class-methods">
|
||
<h3><a class="toc-backref" href="#id58">How do I create static class data and static class methods?</a><a class="headerlink" href="#how-do-i-create-static-class-data-and-static-class-methods" title="Permalink to this headline">¶</a></h3>
|
||
<p>Both static data and static methods (in the sense of C++ or Java) are supported
|
||
in Python.</p>
|
||
<p>For static data, simply define a class attribute. To assign a new value to the
|
||
attribute, you have to explicitly use the class name in the assignment:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
|
||
<span class="n">count</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># number of times C.__init__ called</span>
|
||
|
||
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="n">C</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="n">C</span><span class="o">.</span><span class="n">count</span> <span class="o">+</span> <span class="mi">1</span>
|
||
|
||
<span class="k">def</span> <span class="nf">getcount</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="n">C</span><span class="o">.</span><span class="n">count</span> <span class="c1"># or return self.count</span>
|
||
</pre></div>
|
||
</div>
|
||
<p><code class="docutils literal notranslate"><span class="pre">c.count</span></code> also refers to <code class="docutils literal notranslate"><span class="pre">C.count</span></code> for any <code class="docutils literal notranslate"><span class="pre">c</span></code> such that <code class="docutils literal notranslate"><span class="pre">isinstance(c,</span>
|
||
<span class="pre">C)</span></code> holds, unless overridden by <code class="docutils literal notranslate"><span class="pre">c</span></code> itself or by some class on the base-class
|
||
search path from <code class="docutils literal notranslate"><span class="pre">c.__class__</span></code> back to <code class="docutils literal notranslate"><span class="pre">C</span></code>.</p>
|
||
<p>Caution: within a method of C, an assignment like <code class="docutils literal notranslate"><span class="pre">self.count</span> <span class="pre">=</span> <span class="pre">42</span></code> creates a
|
||
new and unrelated instance named “count” in <code class="docutils literal notranslate"><span class="pre">self</span></code>’s own dict. Rebinding of a
|
||
class-static data name must always specify the class whether inside a method or
|
||
not:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">C</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="mi">314</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Static methods are possible:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
|
||
<span class="nd">@staticmethod</span>
|
||
<span class="k">def</span> <span class="nf">static</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">arg3</span><span class="p">):</span>
|
||
<span class="c1"># No 'self' parameter!</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>However, a far more straightforward way to get the effect of a static method is
|
||
via a simple module-level function:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">getcount</span><span class="p">():</span>
|
||
<span class="k">return</span> <span class="n">C</span><span class="o">.</span><span class="n">count</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If your code is structured so as to define one class (or tightly related class
|
||
hierarchy) per module, this supplies the desired encapsulation.</p>
|
||
</div>
|
||
<div class="section" id="how-can-i-overload-constructors-or-methods-in-python">
|
||
<h3><a class="toc-backref" href="#id59">How can I overload constructors (or methods) in Python?</a><a class="headerlink" href="#how-can-i-overload-constructors-or-methods-in-python" title="Permalink to this headline">¶</a></h3>
|
||
<p>This answer actually applies to all methods, but the question usually comes up
|
||
first in the context of constructors.</p>
|
||
<p>In C++ you’d write</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">class</span> <span class="n">C</span> <span class="p">{</span>
|
||
<span class="n">C</span><span class="p">()</span> <span class="p">{</span> <span class="n">cout</span> <span class="o"><<</span> <span class="s">"No arguments</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span> <span class="p">}</span>
|
||
<span class="n">C</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="p">)</span> <span class="p">{</span> <span class="n">cout</span> <span class="o"><<</span> <span class="s">"Argument is "</span> <span class="o"><<</span> <span class="n">i</span> <span class="o"><<</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span> <span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>In Python you have to write a single constructor that catches all cases using
|
||
default arguments. For example:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
|
||
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
|
||
<span class="k">if</span> <span class="n">i</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"No arguments"</span><span class="p">)</span>
|
||
<span class="k">else</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Argument is"</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This is not entirely equivalent, but close enough in practice.</p>
|
||
<p>You could also try a variable-length argument list, e.g.</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The same approach works for all method definitions.</p>
|
||
</div>
|
||
<div class="section" id="i-try-to-use-spam-and-i-get-an-error-about-someclassname-spam">
|
||
<h3><a class="toc-backref" href="#id60">I try to use __spam and I get an error about _SomeClassName__spam.</a><a class="headerlink" href="#i-try-to-use-spam-and-i-get-an-error-about-someclassname-spam" title="Permalink to this headline">¶</a></h3>
|
||
<p>Variable names with double leading underscores are “mangled” to provide a simple
|
||
but effective way to define class private variables. Any identifier of the form
|
||
<code class="docutils literal notranslate"><span class="pre">__spam</span></code> (at least two leading underscores, at most one trailing underscore)
|
||
is textually replaced with <code class="docutils literal notranslate"><span class="pre">_classname__spam</span></code>, where <code class="docutils literal notranslate"><span class="pre">classname</span></code> is the
|
||
current class name with any leading underscores stripped.</p>
|
||
<p>This doesn’t guarantee privacy: an outside user can still deliberately access
|
||
the “_classname__spam” attribute, and private values are visible in the object’s
|
||
<code class="docutils literal notranslate"><span class="pre">__dict__</span></code>. Many Python programmers never bother to use private variable
|
||
names at all.</p>
|
||
</div>
|
||
<div class="section" id="my-class-defines-del-but-it-is-not-called-when-i-delete-the-object">
|
||
<h3><a class="toc-backref" href="#id61">My class defines __del__ but it is not called when I delete the object.</a><a class="headerlink" href="#my-class-defines-del-but-it-is-not-called-when-i-delete-the-object" title="Permalink to this headline">¶</a></h3>
|
||
<p>There are several possible reasons for this.</p>
|
||
<p>The del statement does not necessarily call <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> – it simply
|
||
decrements the object’s reference count, and if this reaches zero
|
||
<a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> is called.</p>
|
||
<p>If your data structures contain circular links (e.g. a tree where each child has
|
||
a parent reference and each parent has a list of children) the reference counts
|
||
will never go back to zero. Once in a while Python runs an algorithm to detect
|
||
such cycles, but the garbage collector might run some time after the last
|
||
reference to your data structure vanishes, so your <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> method may be
|
||
called at an inconvenient and random time. This is inconvenient if you’re trying
|
||
to reproduce a problem. Worse, the order in which object’s <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a>
|
||
methods are executed is arbitrary. You can run <a class="reference internal" href="../library/gc.html#gc.collect" title="gc.collect"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.collect()</span></code></a> to force a
|
||
collection, but there <em>are</em> pathological cases where objects will never be
|
||
collected.</p>
|
||
<p>Despite the cycle collector, it’s still a good idea to define an explicit
|
||
<code class="docutils literal notranslate"><span class="pre">close()</span></code> method on objects to be called whenever you’re done with them. The
|
||
<code class="docutils literal notranslate"><span class="pre">close()</span></code> method can then remove attributes that refer to subobjects. Don’t
|
||
call <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> directly – <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> should call <code class="docutils literal notranslate"><span class="pre">close()</span></code> and
|
||
<code class="docutils literal notranslate"><span class="pre">close()</span></code> should make sure that it can be called more than once for the same
|
||
object.</p>
|
||
<p>Another way to avoid cyclical references is to use the <a class="reference internal" href="../library/weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> module,
|
||
which allows you to point to objects without incrementing their reference count.
|
||
Tree data structures, for instance, should use weak references for their parent
|
||
and sibling references (if they need them!).</p>
|
||
<p>Finally, if your <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> method raises an exception, a warning message
|
||
is printed to <a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a>.</p>
|
||
</div>
|
||
<div class="section" id="how-do-i-get-a-list-of-all-instances-of-a-given-class">
|
||
<h3><a class="toc-backref" href="#id62">How do I get a list of all instances of a given class?</a><a class="headerlink" href="#how-do-i-get-a-list-of-all-instances-of-a-given-class" title="Permalink to this headline">¶</a></h3>
|
||
<p>Python does not keep track of all instances of a class (or of a built-in type).
|
||
You can program the class’s constructor to keep track of all instances by
|
||
keeping a list of weak references to each instance.</p>
|
||
</div>
|
||
<div class="section" id="why-does-the-result-of-id-appear-to-be-not-unique">
|
||
<h3><a class="toc-backref" href="#id63">Why does the result of <code class="docutils literal notranslate"><span class="pre">id()</span></code> appear to be not unique?</a><a class="headerlink" href="#why-does-the-result-of-id-appear-to-be-not-unique" title="Permalink to this headline">¶</a></h3>
|
||
<p>The <a class="reference internal" href="../library/functions.html#id" title="id"><code class="xref py py-func docutils literal notranslate"><span class="pre">id()</span></code></a> builtin returns an integer that is guaranteed to be unique during
|
||
the lifetime of the object. Since in CPython, this is the object’s memory
|
||
address, it happens frequently that after an object is deleted from memory, the
|
||
next freshly created object is allocated at the same position in memory. This
|
||
is illustrated by this example:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">id</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="c1"># doctest: +SKIP</span>
|
||
<span class="go">13901272</span>
|
||
<span class="gp">>>> </span><span class="nb">id</span><span class="p">(</span><span class="mi">2000</span><span class="p">)</span> <span class="c1"># doctest: +SKIP</span>
|
||
<span class="go">13901272</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The two ids belong to different integer objects that are created before, and
|
||
deleted immediately after execution of the <code class="docutils literal notranslate"><span class="pre">id()</span></code> call. To be sure that
|
||
objects whose id you want to examine are still alive, create another reference
|
||
to the object:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="mi">1000</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">2000</span>
|
||
<span class="gp">>>> </span><span class="nb">id</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c1"># doctest: +SKIP</span>
|
||
<span class="go">13901272</span>
|
||
<span class="gp">>>> </span><span class="nb">id</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="c1"># doctest: +SKIP</span>
|
||
<span class="go">13891296</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="modules">
|
||
<h2><a class="toc-backref" href="#id64">Modules</a><a class="headerlink" href="#modules" title="Permalink to this headline">¶</a></h2>
|
||
<div class="section" id="how-do-i-create-a-pyc-file">
|
||
<h3><a class="toc-backref" href="#id65">How do I create a .pyc file?</a><a class="headerlink" href="#how-do-i-create-a-pyc-file" title="Permalink to this headline">¶</a></h3>
|
||
<p>When a module is imported for the first time (or when the source file has
|
||
changed since the current compiled file was created) a <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> file containing
|
||
the compiled code should be created in a <code class="docutils literal notranslate"><span class="pre">__pycache__</span></code> subdirectory of the
|
||
directory containing the <code class="docutils literal notranslate"><span class="pre">.py</span></code> file. The <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> file will have a
|
||
filename that starts with the same name as the <code class="docutils literal notranslate"><span class="pre">.py</span></code> file, and ends with
|
||
<code class="docutils literal notranslate"><span class="pre">.pyc</span></code>, with a middle component that depends on the particular <code class="docutils literal notranslate"><span class="pre">python</span></code>
|
||
binary that created it. (See <span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> for details.)</p>
|
||
<p>One reason that a <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> file may not be created is a permissions problem
|
||
with the directory containing the source file, meaning that the <code class="docutils literal notranslate"><span class="pre">__pycache__</span></code>
|
||
subdirectory cannot be created. This can happen, for example, if you develop as
|
||
one user but run as another, such as if you are testing with a web server.</p>
|
||
<p>Unless the <span class="target" id="index-4"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONDONTWRITEBYTECODE</span></code></a> environment variable is set,
|
||
creation of a .pyc file is automatic if you’re importing a module and Python
|
||
has the ability (permissions, free space, etc…) to create a <code class="docutils literal notranslate"><span class="pre">__pycache__</span></code>
|
||
subdirectory and write the compiled module to that subdirectory.</p>
|
||
<p>Running Python on a top level script is not considered an import and no
|
||
<code class="docutils literal notranslate"><span class="pre">.pyc</span></code> will be created. For example, if you have a top-level module
|
||
<code class="docutils literal notranslate"><span class="pre">foo.py</span></code> that imports another module <code class="docutils literal notranslate"><span class="pre">xyz.py</span></code>, when you run <code class="docutils literal notranslate"><span class="pre">foo</span></code> (by
|
||
typing <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">foo.py</span></code> as a shell command), a <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> will be created for
|
||
<code class="docutils literal notranslate"><span class="pre">xyz</span></code> because <code class="docutils literal notranslate"><span class="pre">xyz</span></code> is imported, but no <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> file will be created for
|
||
<code class="docutils literal notranslate"><span class="pre">foo</span></code> since <code class="docutils literal notranslate"><span class="pre">foo.py</span></code> isn’t being imported.</p>
|
||
<p>If you need to create a <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> file for <code class="docutils literal notranslate"><span class="pre">foo</span></code> – that is, to create a
|
||
<code class="docutils literal notranslate"><span class="pre">.pyc</span></code> file for a module that is not imported – you can, using the
|
||
<a class="reference internal" href="../library/py_compile.html#module-py_compile" title="py_compile: Generate byte-code files from Python source files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code></a> and <a class="reference internal" href="../library/compileall.html#module-compileall" title="compileall: Tools for byte-compiling all Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compileall</span></code></a> modules.</p>
|
||
<p>The <a class="reference internal" href="../library/py_compile.html#module-py_compile" title="py_compile: Generate byte-code files from Python source files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code></a> module can manually compile any module. One way is to use
|
||
the <code class="docutils literal notranslate"><span class="pre">compile()</span></code> function in that module interactively:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">py_compile</span>
|
||
<span class="gp">>>> </span><span class="n">py_compile</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s1">'foo.py'</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This will write the <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> to a <code class="docutils literal notranslate"><span class="pre">__pycache__</span></code> subdirectory in the same
|
||
location as <code class="docutils literal notranslate"><span class="pre">foo.py</span></code> (or you can override that with the optional parameter
|
||
<code class="docutils literal notranslate"><span class="pre">cfile</span></code>).</p>
|
||
<p>You can also automatically compile all files in a directory or directories using
|
||
the <a class="reference internal" href="../library/compileall.html#module-compileall" title="compileall: Tools for byte-compiling all Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compileall</span></code></a> module. You can do it from the shell prompt by running
|
||
<code class="docutils literal notranslate"><span class="pre">compileall.py</span></code> and providing the path of a directory containing Python files
|
||
to compile:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">compileall</span> <span class="o">.</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="how-do-i-find-the-current-module-name">
|
||
<h3><a class="toc-backref" href="#id66">How do I find the current module name?</a><a class="headerlink" href="#how-do-i-find-the-current-module-name" title="Permalink to this headline">¶</a></h3>
|
||
<p>A module can find out its own module name by looking at the predefined global
|
||
variable <code class="docutils literal notranslate"><span class="pre">__name__</span></code>. If this has the value <code class="docutils literal notranslate"><span class="pre">'__main__'</span></code>, the program is
|
||
running as a script. Many modules that are usually used by importing them also
|
||
provide a command-line interface or a self-test, and only execute this code
|
||
after checking <code class="docutils literal notranslate"><span class="pre">__name__</span></code>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Running test...'</span><span class="p">)</span>
|
||
<span class="o">...</span>
|
||
|
||
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
|
||
<span class="n">main</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="how-can-i-have-modules-that-mutually-import-each-other">
|
||
<h3><a class="toc-backref" href="#id67">How can I have modules that mutually import each other?</a><a class="headerlink" href="#how-can-i-have-modules-that-mutually-import-each-other" title="Permalink to this headline">¶</a></h3>
|
||
<p>Suppose you have the following modules:</p>
|
||
<p>foo.py:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">bar</span> <span class="k">import</span> <span class="n">bar_var</span>
|
||
<span class="n">foo_var</span> <span class="o">=</span> <span class="mi">1</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>bar.py:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">foo</span> <span class="k">import</span> <span class="n">foo_var</span>
|
||
<span class="n">bar_var</span> <span class="o">=</span> <span class="mi">2</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The problem is that the interpreter will perform the following steps:</p>
|
||
<ul class="simple">
|
||
<li><p>main imports foo</p></li>
|
||
<li><p>Empty globals for foo are created</p></li>
|
||
<li><p>foo is compiled and starts executing</p></li>
|
||
<li><p>foo imports bar</p></li>
|
||
<li><p>Empty globals for bar are created</p></li>
|
||
<li><p>bar is compiled and starts executing</p></li>
|
||
<li><p>bar imports foo (which is a no-op since there already is a module named foo)</p></li>
|
||
<li><p>bar.foo_var = foo.foo_var</p></li>
|
||
</ul>
|
||
<p>The last step fails, because Python isn’t done with interpreting <code class="docutils literal notranslate"><span class="pre">foo</span></code> yet and
|
||
the global symbol dictionary for <code class="docutils literal notranslate"><span class="pre">foo</span></code> is still empty.</p>
|
||
<p>The same thing happens when you use <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">foo</span></code>, and then try to access
|
||
<code class="docutils literal notranslate"><span class="pre">foo.foo_var</span></code> in global code.</p>
|
||
<p>There are (at least) three possible workarounds for this problem.</p>
|
||
<p>Guido van Rossum recommends avoiding all uses of <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre"><module></span> <span class="pre">import</span> <span class="pre">...</span></code>,
|
||
and placing all code inside functions. Initializations of global variables and
|
||
class variables should use constants or built-in functions only. This means
|
||
everything from an imported module is referenced as <code class="docutils literal notranslate"><span class="pre"><module>.<name></span></code>.</p>
|
||
<p>Jim Roskind suggests performing steps in the following order in each module:</p>
|
||
<ul class="simple">
|
||
<li><p>exports (globals, functions, and classes that don’t need imported base
|
||
classes)</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">import</span></code> statements</p></li>
|
||
<li><p>active code (including globals that are initialized from imported values).</p></li>
|
||
</ul>
|
||
<p>van Rossum doesn’t like this approach much because the imports appear in a
|
||
strange place, but it does work.</p>
|
||
<p>Matthias Urlichs recommends restructuring your code so that the recursive import
|
||
is not necessary in the first place.</p>
|
||
<p>These solutions are not mutually exclusive.</p>
|
||
</div>
|
||
<div class="section" id="import-x-y-z-returns-module-x-how-do-i-get-z">
|
||
<h3><a class="toc-backref" href="#id68">__import__(‘x.y.z’) returns <module ‘x’>; how do I get z?</a><a class="headerlink" href="#import-x-y-z-returns-module-x-how-do-i-get-z" title="Permalink to this headline">¶</a></h3>
|
||
<p>Consider using the convenience function <a class="reference internal" href="../library/importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code></a> from
|
||
<a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> instead:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">z</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">import_module</span><span class="p">(</span><span class="s1">'x.y.z'</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="when-i-edit-an-imported-module-and-reimport-it-the-changes-don-t-show-up-why-does-this-happen">
|
||
<h3><a class="toc-backref" href="#id69">When I edit an imported module and reimport it, the changes don’t show up. Why does this happen?</a><a class="headerlink" href="#when-i-edit-an-imported-module-and-reimport-it-the-changes-don-t-show-up-why-does-this-happen" title="Permalink to this headline">¶</a></h3>
|
||
<p>For reasons of efficiency as well as consistency, Python only reads the module
|
||
file on the first time a module is imported. If it didn’t, in a program
|
||
consisting of many modules where each one imports the same basic module, the
|
||
basic module would be parsed and re-parsed many times. To force re-reading of a
|
||
changed module, do this:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">importlib</span>
|
||
<span class="kn">import</span> <span class="nn">modname</span>
|
||
<span class="n">importlib</span><span class="o">.</span><span class="n">reload</span><span class="p">(</span><span class="n">modname</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Warning: this technique is not 100% fool-proof. In particular, modules
|
||
containing statements like</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">modname</span> <span class="k">import</span> <span class="n">some_objects</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>will continue to work with the old version of the imported objects. If the
|
||
module contains class definitions, existing class instances will <em>not</em> be
|
||
updated to use the new class definition. This can result in the following
|
||
paradoxical behaviour:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">importlib</span>
|
||
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">cls</span>
|
||
<span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">C</span><span class="p">()</span> <span class="c1"># Create an instance of C</span>
|
||
<span class="gp">>>> </span><span class="n">importlib</span><span class="o">.</span><span class="n">reload</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>
|
||
<span class="go"><module 'cls' from 'cls.py'></span>
|
||
<span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="bp">cls</span><span class="o">.</span><span class="n">C</span><span class="p">)</span> <span class="c1"># isinstance is false?!?</span>
|
||
<span class="go">False</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The nature of the problem is made clear if you print out the “identity” of the
|
||
class objects:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">hex</span><span class="p">(</span><span class="nb">id</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="vm">__class__</span><span class="p">))</span>
|
||
<span class="go">'0x7352a0'</span>
|
||
<span class="gp">>>> </span><span class="nb">hex</span><span class="p">(</span><span class="nb">id</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="n">C</span><span class="p">))</span>
|
||
<span class="go">'0x4198d0'</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</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="#">Programming FAQ</a><ul>
|
||
<li><a class="reference internal" href="#general-questions">General Questions</a></li>
|
||
<li><a class="reference internal" href="#core-language">Core Language</a></li>
|
||
<li><a class="reference internal" href="#numbers-and-strings">Numbers and strings</a></li>
|
||
<li><a class="reference internal" href="#performance">Performance</a></li>
|
||
<li><a class="reference internal" href="#sequences-tuples-lists">Sequences (Tuples/Lists)</a></li>
|
||
<li><a class="reference internal" href="#objects">Objects</a></li>
|
||
<li><a class="reference internal" href="#modules">Modules</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>Previous topic</h4>
|
||
<p class="topless"><a href="general.html"
|
||
title="previous chapter">General Python FAQ</a></p>
|
||
<h4>Next topic</h4>
|
||
<p class="topless"><a href="design.html"
|
||
title="next chapter">Design and History FAQ</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>This Page</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">Report a Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.7/Doc/faq/programming.rst"
|
||
rel="nofollow">Show Source
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</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="design.html" title="Design and History FAQ"
|
||
>next</a> |</li>
|
||
<li class="right" >
|
||
<a href="general.html" title="General Python FAQ"
|
||
>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>
|
||
<span class="language_switcher_placeholder">en</span>
|
||
<span class="version_switcher_placeholder">3.7.4</span>
|
||
<a href="../index.html">Documentation </a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python Frequently Asked Questions</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" style="display: none" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" type="text" name="q" />
|
||
<input type="submit" value="Go" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
<script type="text/javascript">$('.inline-search').show(0);</script>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">Copyright</a> 2001-2019, 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 Jul 13, 2019.
|
||
<a href="../bugs.html">Found a bug</a>?
|
||
<br />
|
||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |