add files
This commit is contained in:
938
python-3.7.4-docs-html/faq/design.html
Normal file
938
python-3.7.4-docs-html/faq/design.html
Normal file
@@ -0,0 +1,938 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Design and History 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="Library and Extension FAQ" href="library.html" />
|
||||
<link rel="prev" title="Programming FAQ" href="programming.html" />
|
||||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/faq/design.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="library.html" title="Library and Extension FAQ"
|
||||
accesskey="N">next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="programming.html" title="Programming 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="design-and-history-faq">
|
||||
<h1><a class="toc-backref" href="#id1">Design and History FAQ</a><a class="headerlink" href="#design-and-history-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="#design-and-history-faq" id="id1">Design and History FAQ</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#why-does-python-use-indentation-for-grouping-of-statements" id="id2">Why does Python use indentation for grouping of statements?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-am-i-getting-strange-results-with-simple-arithmetic-operations" id="id3">Why am I getting strange results with simple arithmetic operations?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-are-floating-point-calculations-so-inaccurate" id="id4">Why are floating-point calculations so inaccurate?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-are-python-strings-immutable" id="id5">Why are Python strings immutable?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-must-self-be-used-explicitly-in-method-definitions-and-calls" id="id6">Why must ‘self’ be used explicitly in method definitions and calls?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-can-t-i-use-an-assignment-in-an-expression" id="id7">Why can’t I use an assignment in an expression?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list" id="id8">Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-is-join-a-string-method-instead-of-a-list-or-tuple-method" id="id9">Why is join() a string method instead of a list or tuple method?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-fast-are-exceptions" id="id10">How fast are exceptions?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-isn-t-there-a-switch-or-case-statement-in-python" id="id11">Why isn’t there a switch or case statement in Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#can-t-you-emulate-threads-in-the-interpreter-instead-of-relying-on-an-os-specific-thread-implementation" id="id12">Can’t you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-can-t-lambda-expressions-contain-statements" id="id13">Why can’t lambda expressions contain statements?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#can-python-be-compiled-to-machine-code-c-or-some-other-language" id="id14">Can Python be compiled to machine code, C or some other language?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-does-python-manage-memory" id="id15">How does Python manage memory?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-doesn-t-cpython-use-a-more-traditional-garbage-collection-scheme" id="id16">Why doesn’t CPython use a more traditional garbage collection scheme?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-isn-t-all-memory-freed-when-cpython-exits" id="id17">Why isn’t all memory freed when CPython exits?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-are-there-separate-tuple-and-list-data-types" id="id18">Why are there separate tuple and list data types?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-are-lists-implemented-in-cpython" id="id19">How are lists implemented in CPython?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-are-dictionaries-implemented-in-cpython" id="id20">How are dictionaries implemented in CPython?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-must-dictionary-keys-be-immutable" id="id21">Why must dictionary keys be immutable?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-doesn-t-list-sort-return-the-sorted-list" id="id22">Why doesn’t list.sort() return the sorted list?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-you-specify-and-enforce-an-interface-spec-in-python" id="id23">How do you specify and enforce an interface spec in Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-is-there-no-goto" id="id24">Why is there no goto?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-can-t-raw-strings-r-strings-end-with-a-backslash" id="id25">Why can’t raw strings (r-strings) end with a backslash?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-doesn-t-python-have-a-with-statement-for-attribute-assignments" id="id26">Why doesn’t Python have a “with” statement for attribute assignments?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-are-colons-required-for-the-if-while-def-class-statements" id="id27">Why are colons required for the if/while/def/class statements?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-does-python-allow-commas-at-the-end-of-lists-and-tuples" id="id28">Why does Python allow commas at the end of lists and tuples?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section" id="why-does-python-use-indentation-for-grouping-of-statements">
|
||||
<h2><a class="toc-backref" href="#id2">Why does Python use indentation for grouping of statements?</a><a class="headerlink" href="#why-does-python-use-indentation-for-grouping-of-statements" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Guido van Rossum believes that using indentation for grouping is extremely
|
||||
elegant and contributes a lot to the clarity of the average Python program.
|
||||
Most people learn to love this feature after a while.</p>
|
||||
<p>Since there are no begin/end brackets there cannot be a disagreement between
|
||||
grouping perceived by the parser and the human reader. Occasionally C
|
||||
programmers will encounter a fragment of code like this:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</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="n">x</span><span class="o">++</span><span class="p">;</span>
|
||||
<span class="n">y</span><span class="o">--</span><span class="p">;</span>
|
||||
<span class="n">z</span><span class="o">++</span><span class="p">;</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Only the <code class="docutils literal notranslate"><span class="pre">x++</span></code> statement is executed if the condition is true, but the
|
||||
indentation leads you to believe otherwise. Even experienced C programmers will
|
||||
sometimes stare at it a long time wondering why <code class="docutils literal notranslate"><span class="pre">y</span></code> is being decremented even
|
||||
for <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">></span> <span class="pre">y</span></code>.</p>
|
||||
<p>Because there are no begin/end brackets, Python is much less prone to
|
||||
coding-style conflicts. In C there are many different ways to place the braces.
|
||||
If you’re used to reading and writing code that uses one style, you will feel at
|
||||
least slightly uneasy when reading (or being required to write) another style.</p>
|
||||
<p>Many coding styles place begin/end brackets on a line by themselves. This makes
|
||||
programs considerably longer and wastes valuable screen space, making it harder
|
||||
to get a good overview of a program. Ideally, a function should fit on one
|
||||
screen (say, 20–30 lines). 20 lines of Python can do a lot more work than 20
|
||||
lines of C. This is not solely due to the lack of begin/end brackets – the
|
||||
lack of declarations and the high-level data types are also responsible – but
|
||||
the indentation-based syntax certainly helps.</p>
|
||||
</div>
|
||||
<div class="section" id="why-am-i-getting-strange-results-with-simple-arithmetic-operations">
|
||||
<h2><a class="toc-backref" href="#id3">Why am I getting strange results with simple arithmetic operations?</a><a class="headerlink" href="#why-am-i-getting-strange-results-with-simple-arithmetic-operations" title="Permalink to this headline">¶</a></h2>
|
||||
<p>See the next question.</p>
|
||||
</div>
|
||||
<div class="section" id="why-are-floating-point-calculations-so-inaccurate">
|
||||
<h2><a class="toc-backref" href="#id4">Why are floating-point calculations so inaccurate?</a><a class="headerlink" href="#why-are-floating-point-calculations-so-inaccurate" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Users are often surprised by results like this:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mf">1.2</span> <span class="o">-</span> <span class="mf">1.0</span>
|
||||
<span class="go">0.19999999999999996</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>and think it is a bug in Python. It’s not. This has little to do with Python,
|
||||
and much more to do with how the underlying platform handles floating-point
|
||||
numbers.</p>
|
||||
<p>The <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> type in CPython uses a C <code class="docutils literal notranslate"><span class="pre">double</span></code> for storage. A
|
||||
<a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> object’s value is stored in binary floating-point with a fixed
|
||||
precision (typically 53 bits) and Python uses C operations, which in turn rely
|
||||
on the hardware implementation in the processor, to perform floating-point
|
||||
operations. This means that as far as floating-point operations are concerned,
|
||||
Python behaves like many popular languages including C and Java.</p>
|
||||
<p>Many numbers that can be written easily in decimal notation cannot be expressed
|
||||
exactly in binary floating-point. For example, after:</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="mf">1.2</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>the value stored for <code class="docutils literal notranslate"><span class="pre">x</span></code> is a (very good) approximation to the decimal value
|
||||
<code class="docutils literal notranslate"><span class="pre">1.2</span></code>, but is not exactly equal to it. On a typical machine, the actual
|
||||
stored value is:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">1.0011001100110011001100110011001100110011001100110011</span> <span class="p">(</span><span class="n">binary</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>which is exactly:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">1.1999999999999999555910790149937383830547332763671875</span> <span class="p">(</span><span class="n">decimal</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The typical precision of 53 bits provides Python floats with 15–16
|
||||
decimal digits of accuracy.</p>
|
||||
<p>For a fuller explanation, please see the <a class="reference internal" href="../tutorial/floatingpoint.html#tut-fp-issues"><span class="std std-ref">floating point arithmetic</span></a> chapter in the Python tutorial.</p>
|
||||
</div>
|
||||
<div class="section" id="why-are-python-strings-immutable">
|
||||
<h2><a class="toc-backref" href="#id5">Why are Python strings immutable?</a><a class="headerlink" href="#why-are-python-strings-immutable" title="Permalink to this headline">¶</a></h2>
|
||||
<p>There are several advantages.</p>
|
||||
<p>One is performance: knowing that a string is immutable means we can allocate
|
||||
space for it at creation time, and the storage requirements are fixed and
|
||||
unchanging. This is also one of the reasons for the distinction between tuples
|
||||
and lists.</p>
|
||||
<p>Another advantage is that strings in Python are considered as “elemental” as
|
||||
numbers. No amount of activity will change the value 8 to anything else, and in
|
||||
Python, no amount of activity will change the string “eight” to anything else.</p>
|
||||
</div>
|
||||
<div class="section" id="why-must-self-be-used-explicitly-in-method-definitions-and-calls">
|
||||
<span id="why-self"></span><h2><a class="toc-backref" href="#id6">Why must ‘self’ be used explicitly in method definitions and calls?</a><a class="headerlink" href="#why-must-self-be-used-explicitly-in-method-definitions-and-calls" title="Permalink to this headline">¶</a></h2>
|
||||
<p>The idea was borrowed from Modula-3. It turns out to be very useful, for a
|
||||
variety of reasons.</p>
|
||||
<p>First, it’s more obvious that you are using a method or instance attribute
|
||||
instead of a local variable. Reading <code class="docutils literal notranslate"><span class="pre">self.x</span></code> or <code class="docutils literal notranslate"><span class="pre">self.meth()</span></code> makes it
|
||||
absolutely clear that an instance variable or method is used even if you don’t
|
||||
know the class definition by heart. In C++, you can sort of tell by the lack of
|
||||
a local variable declaration (assuming globals are rare or easily recognizable)
|
||||
– but in Python, there are no local variable declarations, so you’d have to
|
||||
look up the class definition to be sure. Some C++ and Java coding standards
|
||||
call for instance attributes to have an <code class="docutils literal notranslate"><span class="pre">m_</span></code> prefix, so this explicitness is
|
||||
still useful in those languages, too.</p>
|
||||
<p>Second, it means that no special syntax is necessary if you want to explicitly
|
||||
reference or call the method from a particular class. In C++, if you want to
|
||||
use a method from a base class which is overridden in a derived class, you have
|
||||
to use the <code class="docutils literal notranslate"><span class="pre">::</span></code> operator – in Python you can write
|
||||
<code class="docutils literal notranslate"><span class="pre">baseclass.methodname(self,</span> <span class="pre"><argument</span> <span class="pre">list>)</span></code>. This is particularly useful
|
||||
for <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code></a> methods, and in general in cases where a derived class
|
||||
method wants to extend the base class method of the same name and thus has to
|
||||
call the base class method somehow.</p>
|
||||
<p>Finally, for instance variables it solves a syntactic problem with assignment:
|
||||
since local variables in Python are (by definition!) those variables to which a
|
||||
value is assigned in a function body (and that aren’t explicitly declared
|
||||
global), there has to be some way to tell the interpreter that an assignment was
|
||||
meant to assign to an instance variable instead of to a local variable, and it
|
||||
should preferably be syntactic (for efficiency reasons). C++ does this through
|
||||
declarations, but Python doesn’t have declarations and it would be a pity having
|
||||
to introduce them just for this purpose. Using the explicit <code class="docutils literal notranslate"><span class="pre">self.var</span></code> solves
|
||||
this nicely. Similarly, for using instance variables, having to write
|
||||
<code class="docutils literal notranslate"><span class="pre">self.var</span></code> means that references to unqualified names inside a method don’t
|
||||
have to search the instance’s directories. To put it another way, local
|
||||
variables and instance variables live in two different namespaces, and you need
|
||||
to tell Python which namespace to use.</p>
|
||||
</div>
|
||||
<div class="section" id="why-can-t-i-use-an-assignment-in-an-expression">
|
||||
<h2><a class="toc-backref" href="#id7">Why can’t I use an assignment in an expression?</a><a class="headerlink" href="#why-can-t-i-use-an-assignment-in-an-expression" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Many people used to C or Perl complain that they want to use this C idiom:</p>
|
||||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="p">(</span><span class="n">line</span> <span class="o">=</span> <span class="n">readline</span><span class="p">(</span><span class="n">f</span><span class="p">))</span> <span class="p">{</span>
|
||||
<span class="c1">// do something with line</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>where in Python you’re forced to write this:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
|
||||
<span class="n">line</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
|
||||
<span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="p">:</span>
|
||||
<span class="k">break</span>
|
||||
<span class="o">...</span> <span class="c1"># do something with line</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The reason for not allowing assignment in Python expressions is a common,
|
||||
hard-to-find bug in those other languages, caused by this construct:</p>
|
||||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
|
||||
<span class="c1">// error handling</span>
|
||||
<span class="p">}</span>
|
||||
<span class="k">else</span> <span class="p">{</span>
|
||||
<span class="c1">// code that only works for nonzero x</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The error is a simple typo: <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">0</span></code>, which assigns 0 to the variable <code class="docutils literal notranslate"><span class="pre">x</span></code>,
|
||||
was written while the comparison <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">0</span></code> is certainly what was intended.</p>
|
||||
<p>Many alternatives have been proposed. Most are hacks that save some typing but
|
||||
use arbitrary or cryptic syntax or keywords, and fail the simple criterion for
|
||||
language change proposals: it should intuitively suggest the proper meaning to a
|
||||
human reader who has not yet been introduced to the construct.</p>
|
||||
<p>An interesting phenomenon is that most experienced Python programmers recognize
|
||||
the <code class="docutils literal notranslate"><span class="pre">while</span> <span class="pre">True</span></code> idiom and don’t seem to be missing the assignment in
|
||||
expression construct much; it’s only newcomers who express a strong desire to
|
||||
add this to the language.</p>
|
||||
<p>There’s an alternative way of spelling this that seems attractive but is
|
||||
generally less robust than the “while True” solution:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">line</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
|
||||
<span class="k">while</span> <span class="n">line</span><span class="p">:</span>
|
||||
<span class="o">...</span> <span class="c1"># do something with line...</span>
|
||||
<span class="n">line</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The problem with this is that if you change your mind about exactly how you get
|
||||
the next line (e.g. you want to change it into <code class="docutils literal notranslate"><span class="pre">sys.stdin.readline()</span></code>) you
|
||||
have to remember to change two places in your program – the second occurrence
|
||||
is hidden at the bottom of the loop.</p>
|
||||
<p>The best approach is to use iterators, making it possible to loop through
|
||||
objects using the <code class="docutils literal notranslate"><span class="pre">for</span></code> statement. For example, <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file objects</span></a> support the iterator protocol, so you can write simply:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
|
||||
<span class="o">...</span> <span class="c1"># do something with line...</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list">
|
||||
<h2><a class="toc-backref" href="#id8">Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?</a><a class="headerlink" href="#why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list" title="Permalink to this headline">¶</a></h2>
|
||||
<p>As Guido said:</p>
|
||||
<blockquote>
|
||||
<div><p>(a) For some operations, prefix notation just reads better than
|
||||
postfix – prefix (and infix!) operations have a long tradition in
|
||||
mathematics which likes notations where the visuals help the
|
||||
mathematician thinking about a problem. Compare the easy with which we
|
||||
rewrite a formula like x*(a+b) into x*a + x*b to the clumsiness of
|
||||
doing the same thing using a raw OO notation.</p>
|
||||
<p>(b) When I read code that says len(x) I <em>know</em> that it is asking for
|
||||
the length of something. This tells me two things: the result is an
|
||||
integer, and the argument is some kind of container. To the contrary,
|
||||
when I read x.len(), I have to already know that x is some kind of
|
||||
container implementing an interface or inheriting from a class that
|
||||
has a standard len(). Witness the confusion we occasionally have when
|
||||
a class that is not implementing a mapping has a get() or keys()
|
||||
method, or something that isn’t a file has a write() method.</p>
|
||||
<p class="attribution">—<a class="reference external" href="https://mail.python.org/pipermail/python-3000/2006-November/004643.html">https://mail.python.org/pipermail/python-3000/2006-November/004643.html</a></p>
|
||||
</div></blockquote>
|
||||
</div>
|
||||
<div class="section" id="why-is-join-a-string-method-instead-of-a-list-or-tuple-method">
|
||||
<h2><a class="toc-backref" href="#id9">Why is join() a string method instead of a list or tuple method?</a><a class="headerlink" href="#why-is-join-a-string-method-instead-of-a-list-or-tuple-method" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Strings became much more like other standard types starting in Python 1.6, when
|
||||
methods were added which give the same functionality that has always been
|
||||
available using the functions of the string module. Most of these new methods
|
||||
have been widely accepted, but the one which appears to make some programmers
|
||||
feel uncomfortable is:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="s2">", "</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s1">'1'</span><span class="p">,</span> <span class="s1">'2'</span><span class="p">,</span> <span class="s1">'4'</span><span class="p">,</span> <span class="s1">'8'</span><span class="p">,</span> <span class="s1">'16'</span><span class="p">])</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>which gives the result:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="s2">"1, 2, 4, 8, 16"</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>There are two common arguments against this usage.</p>
|
||||
<p>The first runs along the lines of: “It looks really ugly using a method of a
|
||||
string literal (string constant)”, to which the answer is that it might, but a
|
||||
string literal is just a fixed value. If the methods are to be allowed on names
|
||||
bound to strings there is no logical reason to make them unavailable on
|
||||
literals.</p>
|
||||
<p>The second objection is typically cast as: “I am really telling a sequence to
|
||||
join its members together with a string constant”. Sadly, you aren’t. For some
|
||||
reason there seems to be much less difficulty with having <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> as
|
||||
a string method, since in that case it is easy to see that</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="s2">"1, 2, 4, 8, 16"</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">", "</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>is an instruction to a string literal to return the substrings delimited by the
|
||||
given separator (or, by default, arbitrary runs of white space).</p>
|
||||
<p><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">join()</span></code></a> is a string method because in using it you are telling the
|
||||
separator string to iterate over a sequence of strings and insert itself between
|
||||
adjacent elements. This method can be used with any argument which obeys the
|
||||
rules for sequence objects, including any new classes you might define yourself.
|
||||
Similar methods exist for bytes and bytearray objects.</p>
|
||||
</div>
|
||||
<div class="section" id="how-fast-are-exceptions">
|
||||
<h2><a class="toc-backref" href="#id10">How fast are exceptions?</a><a class="headerlink" href="#how-fast-are-exceptions" title="Permalink to this headline">¶</a></h2>
|
||||
<p>A try/except block is extremely efficient if no exceptions are raised. Actually
|
||||
catching an exception is expensive. In versions of Python prior to 2.0 it was
|
||||
common to use this idiom:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
|
||||
<span class="n">value</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="k">except</span> <span class="ne">KeyError</span><span class="p">:</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">getvalue</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
||||
<span class="n">value</span> <span class="o">=</span> <span class="n">mydict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>This only made sense when you expected the dict to have the key almost all the
|
||||
time. If that wasn’t the case, you coded it like this:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">mydict</span><span class="p">:</span>
|
||||
<span class="n">value</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="k">else</span><span class="p">:</span>
|
||||
<span class="n">value</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">getvalue</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>For this specific case, you could also use <code class="docutils literal notranslate"><span class="pre">value</span> <span class="pre">=</span> <span class="pre">dict.setdefault(key,</span>
|
||||
<span class="pre">getvalue(key))</span></code>, but only if the <code class="docutils literal notranslate"><span class="pre">getvalue()</span></code> call is cheap enough because it
|
||||
is evaluated in all cases.</p>
|
||||
</div>
|
||||
<div class="section" id="why-isn-t-there-a-switch-or-case-statement-in-python">
|
||||
<h2><a class="toc-backref" href="#id11">Why isn’t there a switch or case statement in Python?</a><a class="headerlink" href="#why-isn-t-there-a-switch-or-case-statement-in-python" title="Permalink to this headline">¶</a></h2>
|
||||
<p>You can do this easily enough with a sequence of <code class="docutils literal notranslate"><span class="pre">if...</span> <span class="pre">elif...</span> <span class="pre">elif...</span> <span class="pre">else</span></code>.
|
||||
There have been some proposals for switch statement syntax, but there is no
|
||||
consensus (yet) on whether and how to do range tests. See <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0275"><strong>PEP 275</strong></a> for
|
||||
complete details and the current status.</p>
|
||||
<p>For cases where you need to choose from a very large number of possibilities,
|
||||
you can create a dictionary mapping case values to functions to call. For
|
||||
example:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">function_1</span><span class="p">(</span><span class="o">...</span><span class="p">):</span>
|
||||
<span class="o">...</span>
|
||||
|
||||
<span class="n">functions</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'a'</span><span class="p">:</span> <span class="n">function_1</span><span class="p">,</span>
|
||||
<span class="s1">'b'</span><span class="p">:</span> <span class="n">function_2</span><span class="p">,</span>
|
||||
<span class="s1">'c'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">method_1</span><span class="p">,</span> <span class="o">...</span><span class="p">}</span>
|
||||
|
||||
<span class="n">func</span> <span class="o">=</span> <span class="n">functions</span><span class="p">[</span><span class="n">value</span><span class="p">]</span>
|
||||
<span class="n">func</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>For calling methods on objects, you can simplify yet further by using the
|
||||
<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> built-in to retrieve methods with a particular name:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">visit_a</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">...</span><span class="p">):</span>
|
||||
<span class="o">...</span>
|
||||
<span class="o">...</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">dispatch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
|
||||
<span class="n">method_name</span> <span class="o">=</span> <span class="s1">'visit_'</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
|
||||
<span class="n">method</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">method_name</span><span class="p">)</span>
|
||||
<span class="n">method</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>It’s suggested that you use a prefix for the method names, such as <code class="docutils literal notranslate"><span class="pre">visit_</span></code> in
|
||||
this example. Without such a prefix, if values are coming from an untrusted
|
||||
source, an attacker would be able to call any method on your object.</p>
|
||||
</div>
|
||||
<div class="section" id="can-t-you-emulate-threads-in-the-interpreter-instead-of-relying-on-an-os-specific-thread-implementation">
|
||||
<h2><a class="toc-backref" href="#id12">Can’t you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?</a><a class="headerlink" href="#can-t-you-emulate-threads-in-the-interpreter-instead-of-relying-on-an-os-specific-thread-implementation" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Answer 1: Unfortunately, the interpreter pushes at least one C stack frame for
|
||||
each Python stack frame. Also, extensions can call back into Python at almost
|
||||
random moments. Therefore, a complete threads implementation requires thread
|
||||
support for C.</p>
|
||||
<p>Answer 2: Fortunately, there is <a class="reference external" href="https://github.com/stackless-dev/stackless/wiki">Stackless Python</a>,
|
||||
which has a completely redesigned interpreter loop that avoids the C stack.</p>
|
||||
</div>
|
||||
<div class="section" id="why-can-t-lambda-expressions-contain-statements">
|
||||
<h2><a class="toc-backref" href="#id13">Why can’t lambda expressions contain statements?</a><a class="headerlink" href="#why-can-t-lambda-expressions-contain-statements" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Python lambda expressions cannot contain statements because Python’s syntactic
|
||||
framework can’t handle statements nested inside expressions. However, in
|
||||
Python, this is not a serious problem. Unlike lambda forms in other languages,
|
||||
where they add functionality, Python lambdas are only a shorthand notation if
|
||||
you’re too lazy to define a function.</p>
|
||||
<p>Functions are already first class objects in Python, and can be declared in a
|
||||
local scope. Therefore the only advantage of using a lambda instead of a
|
||||
locally-defined function is that you don’t need to invent a name for the
|
||||
function – but that’s just a local variable to which the function object (which
|
||||
is exactly the same type of object that a lambda expression yields) is assigned!</p>
|
||||
</div>
|
||||
<div class="section" id="can-python-be-compiled-to-machine-code-c-or-some-other-language">
|
||||
<h2><a class="toc-backref" href="#id14">Can Python be compiled to machine code, C or some other language?</a><a class="headerlink" href="#can-python-be-compiled-to-machine-code-c-or-some-other-language" title="Permalink to this headline">¶</a></h2>
|
||||
<p><a class="reference external" href="http://cython.org/">Cython</a> compiles a modified version of Python with
|
||||
optional annotations into C extensions. <a class="reference external" href="http://www.nuitka.net/">Nuitka</a> is
|
||||
an up-and-coming compiler of Python into C++ code, aiming to support the full
|
||||
Python language. For compiling to Java you can consider
|
||||
<a class="reference external" href="https://voc.readthedocs.io">VOC</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="how-does-python-manage-memory">
|
||||
<h2><a class="toc-backref" href="#id15">How does Python manage memory?</a><a class="headerlink" href="#how-does-python-manage-memory" title="Permalink to this headline">¶</a></h2>
|
||||
<p>The details of Python memory management depend on the implementation. The
|
||||
standard implementation of Python, <a class="reference internal" href="../glossary.html#term-cpython"><span class="xref std std-term">CPython</span></a>, uses reference counting to
|
||||
detect inaccessible objects, and another mechanism to collect reference cycles,
|
||||
periodically executing a cycle detection algorithm which looks for inaccessible
|
||||
cycles and deletes the objects involved. The <a class="reference internal" href="../library/gc.html#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code></a> module provides functions
|
||||
to perform a garbage collection, obtain debugging statistics, and tune the
|
||||
collector’s parameters.</p>
|
||||
<p>Other implementations (such as <a class="reference external" href="http://www.jython.org">Jython</a> or
|
||||
<a class="reference external" href="http://www.pypy.org">PyPy</a>), however, can rely on a different mechanism
|
||||
such as a full-blown garbage collector. This difference can cause some
|
||||
subtle porting problems if your Python code depends on the behavior of the
|
||||
reference counting implementation.</p>
|
||||
<p>In some Python implementations, the following code (which is fine in CPython)
|
||||
will probably run out of file descriptors:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">very_long_list_of_files</span><span class="p">:</span>
|
||||
<span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
|
||||
<span class="n">c</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Indeed, using CPython’s reference counting and destructor scheme, each new
|
||||
assignment to <em>f</em> closes the previous file. With a traditional GC, however,
|
||||
those file objects will only get collected (and closed) at varying and possibly
|
||||
long intervals.</p>
|
||||
<p>If you want to write code that will work with any Python implementation,
|
||||
you should explicitly close the file or use the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement;
|
||||
this will work regardless of memory management scheme:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">very_long_list_of_files</span><span class="p">:</span>
|
||||
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
|
||||
<span class="n">c</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="why-doesn-t-cpython-use-a-more-traditional-garbage-collection-scheme">
|
||||
<h2><a class="toc-backref" href="#id16">Why doesn’t CPython use a more traditional garbage collection scheme?</a><a class="headerlink" href="#why-doesn-t-cpython-use-a-more-traditional-garbage-collection-scheme" title="Permalink to this headline">¶</a></h2>
|
||||
<p>For one thing, this is not a C standard feature and hence it’s not portable.
|
||||
(Yes, we know about the Boehm GC library. It has bits of assembler code for
|
||||
<em>most</em> common platforms, not for all of them, and although it is mostly
|
||||
transparent, it isn’t completely transparent; patches are required to get
|
||||
Python to work with it.)</p>
|
||||
<p>Traditional GC also becomes a problem when Python is embedded into other
|
||||
applications. While in a standalone Python it’s fine to replace the standard
|
||||
malloc() and free() with versions provided by the GC library, an application
|
||||
embedding Python may want to have its <em>own</em> substitute for malloc() and free(),
|
||||
and may not want Python’s. Right now, CPython works with anything that
|
||||
implements malloc() and free() properly.</p>
|
||||
</div>
|
||||
<div class="section" id="why-isn-t-all-memory-freed-when-cpython-exits">
|
||||
<h2><a class="toc-backref" href="#id17">Why isn’t all memory freed when CPython exits?</a><a class="headerlink" href="#why-isn-t-all-memory-freed-when-cpython-exits" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Objects referenced from the global namespaces of Python modules are not always
|
||||
deallocated when Python exits. This may happen if there are circular
|
||||
references. There are also certain bits of memory that are allocated by the C
|
||||
library that are impossible to free (e.g. a tool like Purify will complain about
|
||||
these). Python is, however, aggressive about cleaning up memory on exit and
|
||||
does try to destroy every single object.</p>
|
||||
<p>If you want to force Python to delete certain things on deallocation use the
|
||||
<a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a> module to run a function that will force those deletions.</p>
|
||||
</div>
|
||||
<div class="section" id="why-are-there-separate-tuple-and-list-data-types">
|
||||
<h2><a class="toc-backref" href="#id18">Why are there separate tuple and list data types?</a><a class="headerlink" href="#why-are-there-separate-tuple-and-list-data-types" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Lists and tuples, while similar in many respects, are generally used in
|
||||
fundamentally different ways. Tuples can be thought of as being similar to
|
||||
Pascal records or C structs; they’re small collections of related data which may
|
||||
be of different types which are operated on as a group. For example, a
|
||||
Cartesian coordinate is appropriately represented as a tuple of two or three
|
||||
numbers.</p>
|
||||
<p>Lists, on the other hand, are more like arrays in other languages. They tend to
|
||||
hold a varying number of objects all of which have the same type and which are
|
||||
operated on one-by-one. For example, <code class="docutils literal notranslate"><span class="pre">os.listdir('.')</span></code> returns a list of
|
||||
strings representing the files in the current directory. Functions which
|
||||
operate on this output would generally not break if you added another file or
|
||||
two to the directory.</p>
|
||||
<p>Tuples are immutable, meaning that once a tuple has been created, you can’t
|
||||
replace any of its elements with a new value. Lists are mutable, meaning that
|
||||
you can always change a list’s elements. Only immutable elements can be used as
|
||||
dictionary keys, and hence only tuples and not lists can be used as keys.</p>
|
||||
</div>
|
||||
<div class="section" id="how-are-lists-implemented-in-cpython">
|
||||
<h2><a class="toc-backref" href="#id19">How are lists implemented in CPython?</a><a class="headerlink" href="#how-are-lists-implemented-in-cpython" title="Permalink to this headline">¶</a></h2>
|
||||
<p>CPython’s lists are really variable-length arrays, not Lisp-style linked lists.
|
||||
The implementation uses a contiguous array of references to other objects, and
|
||||
keeps a pointer to this array and the array’s length in a list head structure.</p>
|
||||
<p>This makes indexing a list <code class="docutils literal notranslate"><span class="pre">a[i]</span></code> an operation whose cost is independent of
|
||||
the size of the list or the value of the index.</p>
|
||||
<p>When items are appended or inserted, the array of references is resized. Some
|
||||
cleverness is applied to improve the performance of appending items repeatedly;
|
||||
when the array must be grown, some extra space is allocated so the next few
|
||||
times don’t require an actual resize.</p>
|
||||
</div>
|
||||
<div class="section" id="how-are-dictionaries-implemented-in-cpython">
|
||||
<h2><a class="toc-backref" href="#id20">How are dictionaries implemented in CPython?</a><a class="headerlink" href="#how-are-dictionaries-implemented-in-cpython" title="Permalink to this headline">¶</a></h2>
|
||||
<p>CPython’s dictionaries are implemented as resizable hash tables. Compared to
|
||||
B-trees, this gives better performance for lookup (the most common operation by
|
||||
far) under most circumstances, and the implementation is simpler.</p>
|
||||
<p>Dictionaries work by computing a hash code for each key stored in the dictionary
|
||||
using the <a class="reference internal" href="../library/functions.html#hash" title="hash"><code class="xref py py-func docutils literal notranslate"><span class="pre">hash()</span></code></a> built-in function. The hash code varies widely depending
|
||||
on the key and a per-process seed; for example, “Python” could hash to
|
||||
-539294296 while “python”, a string that differs by a single bit, could hash
|
||||
to 1142331976. The hash code is then used to calculate a location in an
|
||||
internal array where the value will be stored. Assuming that you’re storing
|
||||
keys that all have different hash values, this means that dictionaries take
|
||||
constant time – O(1), in Big-O notation – to retrieve a key.</p>
|
||||
</div>
|
||||
<div class="section" id="why-must-dictionary-keys-be-immutable">
|
||||
<h2><a class="toc-backref" href="#id21">Why must dictionary keys be immutable?</a><a class="headerlink" href="#why-must-dictionary-keys-be-immutable" title="Permalink to this headline">¶</a></h2>
|
||||
<p>The hash table implementation of dictionaries uses a hash value calculated from
|
||||
the key value to find the key. If the key were a mutable object, its value
|
||||
could change, and thus its hash could also change. But since whoever changes
|
||||
the key object can’t tell that it was being used as a dictionary key, it can’t
|
||||
move the entry around in the dictionary. Then, when you try to look up the same
|
||||
object in the dictionary it won’t be found because its hash value is different.
|
||||
If you tried to look up the old value it wouldn’t be found either, because the
|
||||
value of the object found in that hash bin would be different.</p>
|
||||
<p>If you want a dictionary indexed with a list, simply convert the list to a tuple
|
||||
first; the function <code class="docutils literal notranslate"><span class="pre">tuple(L)</span></code> creates a tuple with the same entries as the
|
||||
list <code class="docutils literal notranslate"><span class="pre">L</span></code>. Tuples are immutable and can therefore be used as dictionary keys.</p>
|
||||
<p>Some unacceptable solutions that have been proposed:</p>
|
||||
<ul>
|
||||
<li><p>Hash lists by their address (object ID). This doesn’t work because if you
|
||||
construct a new list with the same value it won’t be found; e.g.:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">mydict</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="s1">'12'</span><span class="p">}</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">mydict</span><span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]])</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>would raise a <a class="reference internal" href="../library/exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception because the id of the <code class="docutils literal notranslate"><span class="pre">[1,</span> <span class="pre">2]</span></code> used in the
|
||||
second line differs from that in the first line. In other words, dictionary
|
||||
keys should be compared using <code class="docutils literal notranslate"><span class="pre">==</span></code>, not using <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>.</p>
|
||||
</li>
|
||||
<li><p>Make a copy when using a list as a key. This doesn’t work because the list,
|
||||
being a mutable object, could contain a reference to itself, and then the
|
||||
copying code would run into an infinite loop.</p></li>
|
||||
<li><p>Allow lists as keys but tell the user not to modify them. This would allow a
|
||||
class of hard-to-track bugs in programs when you forgot or modified a list by
|
||||
accident. It also invalidates an important invariant of dictionaries: every
|
||||
value in <code class="docutils literal notranslate"><span class="pre">d.keys()</span></code> is usable as a key of the dictionary.</p></li>
|
||||
<li><p>Mark lists as read-only once they are used as a dictionary key. The problem
|
||||
is that it’s not just the top-level object that could change its value; you
|
||||
could use a tuple containing a list as a key. Entering anything as a key into
|
||||
a dictionary would require marking all objects reachable from there as
|
||||
read-only – and again, self-referential objects could cause an infinite loop.</p></li>
|
||||
</ul>
|
||||
<p>There is a trick to get around this if you need to, but use it at your own risk:
|
||||
You can wrap a mutable structure inside a class instance which has both a
|
||||
<a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code></a> and a <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code></a> method. You must then make sure that the
|
||||
hash value for all such wrapper objects that reside in a dictionary (or other
|
||||
hash based structure), remain fixed while the object is in the dictionary (or
|
||||
other structure).</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ListWrapper</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">the_list</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">the_list</span> <span class="o">=</span> <span class="n">the_list</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
|
||||
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">the_list</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">the_list</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="n">l</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">the_list</span>
|
||||
<span class="n">result</span> <span class="o">=</span> <span class="mi">98767</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">l</span><span class="p">)</span><span class="o">*</span><span class="mi">555</span>
|
||||
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">el</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">l</span><span class="p">):</span>
|
||||
<span class="k">try</span><span class="p">:</span>
|
||||
<span class="n">result</span> <span class="o">=</span> <span class="n">result</span> <span class="o">+</span> <span class="p">(</span><span class="nb">hash</span><span class="p">(</span><span class="n">el</span><span class="p">)</span> <span class="o">%</span> <span class="mi">9999999</span><span class="p">)</span> <span class="o">*</span> <span class="mi">1001</span> <span class="o">+</span> <span class="n">i</span>
|
||||
<span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
|
||||
<span class="n">result</span> <span class="o">=</span> <span class="p">(</span><span class="n">result</span> <span class="o">%</span> <span class="mi">7777777</span><span class="p">)</span> <span class="o">+</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">333</span>
|
||||
<span class="k">return</span> <span class="n">result</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Note that the hash computation is complicated by the possibility that some
|
||||
members of the list may be unhashable and also by the possibility of arithmetic
|
||||
overflow.</p>
|
||||
<p>Furthermore it must always be the case that if <code class="docutils literal notranslate"><span class="pre">o1</span> <span class="pre">==</span> <span class="pre">o2</span></code> (ie <code class="docutils literal notranslate"><span class="pre">o1.__eq__(o2)</span>
|
||||
<span class="pre">is</span> <span class="pre">True</span></code>) then <code class="docutils literal notranslate"><span class="pre">hash(o1)</span> <span class="pre">==</span> <span class="pre">hash(o2)</span></code> (ie, <code class="docutils literal notranslate"><span class="pre">o1.__hash__()</span> <span class="pre">==</span> <span class="pre">o2.__hash__()</span></code>),
|
||||
regardless of whether the object is in a dictionary or not. If you fail to meet
|
||||
these restrictions dictionaries and other hash based structures will misbehave.</p>
|
||||
<p>In the case of ListWrapper, whenever the wrapper object is in a dictionary the
|
||||
wrapped list must not change to avoid anomalies. Don’t do this unless you are
|
||||
prepared to think hard about the requirements and the consequences of not
|
||||
meeting them correctly. Consider yourself warned.</p>
|
||||
</div>
|
||||
<div class="section" id="why-doesn-t-list-sort-return-the-sorted-list">
|
||||
<h2><a class="toc-backref" href="#id22">Why doesn’t list.sort() return the sorted list?</a><a class="headerlink" href="#why-doesn-t-list-sort-return-the-sorted-list" title="Permalink to this headline">¶</a></h2>
|
||||
<p>In situations where performance matters, making a copy of the list just to sort
|
||||
it would be wasteful. Therefore, <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> sorts the list in place. In
|
||||
order to remind you of that fact, it does not return the sorted list. This way,
|
||||
you won’t be fooled into accidentally overwriting a list when you need a sorted
|
||||
copy but also need to keep the unsorted version around.</p>
|
||||
<p>If you want to return a new list, use the built-in <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
|
||||
instead. This function creates a new list from a provided iterable, sorts
|
||||
it and returns it. For example, here’s how to iterate over the keys of a
|
||||
dictionary in sorted order:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">mydict</span><span class="p">):</span>
|
||||
<span class="o">...</span> <span class="c1"># do whatever with mydict[key]...</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="how-do-you-specify-and-enforce-an-interface-spec-in-python">
|
||||
<h2><a class="toc-backref" href="#id23">How do you specify and enforce an interface spec in Python?</a><a class="headerlink" href="#how-do-you-specify-and-enforce-an-interface-spec-in-python" title="Permalink to this headline">¶</a></h2>
|
||||
<p>An interface specification for a module as provided by languages such as C++ and
|
||||
Java describes the prototypes for the methods and functions of the module. Many
|
||||
feel that compile-time enforcement of interface specifications helps in the
|
||||
construction of large programs.</p>
|
||||
<p>Python 2.6 adds an <a class="reference internal" href="../library/abc.html#module-abc" title="abc: Abstract base classes according to PEP 3119."><code class="xref py py-mod docutils literal notranslate"><span class="pre">abc</span></code></a> module that lets you define Abstract Base Classes
|
||||
(ABCs). You can then 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> and <a class="reference internal" href="../library/functions.html#issubclass" title="issubclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubclass()</span></code></a> to check
|
||||
whether an instance or a class implements a particular ABC. The
|
||||
<a class="reference internal" href="../library/collections.abc.html#module-collections.abc" title="collections.abc: Abstract base classes for containers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections.abc</span></code></a> module defines a set of useful ABCs such as
|
||||
<a class="reference internal" href="../library/collections.abc.html#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Iterable</span></code></a>, <a class="reference internal" href="../library/collections.abc.html#collections.abc.Container" title="collections.abc.Container"><code class="xref py py-class docutils literal notranslate"><span class="pre">Container</span></code></a>, and
|
||||
<a class="reference internal" href="../library/collections.abc.html#collections.abc.MutableMapping" title="collections.abc.MutableMapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">MutableMapping</span></code></a>.</p>
|
||||
<p>For Python, many of the advantages of interface specifications can be obtained
|
||||
by an appropriate test discipline for components. There is also a tool,
|
||||
PyChecker, which can be used to find problems due to subclassing.</p>
|
||||
<p>A good test suite for a module can both provide a regression test and serve as a
|
||||
module interface specification and a set of examples. Many Python modules can
|
||||
be run as a script to provide a simple “self test.” Even modules which use
|
||||
complex external interfaces can often be tested in isolation using trivial
|
||||
“stub” emulations of the external interface. The <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> and
|
||||
<a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> modules or third-party test frameworks can be used to construct
|
||||
exhaustive test suites that exercise every line of code in a module.</p>
|
||||
<p>An appropriate testing discipline can help build large complex applications in
|
||||
Python as well as having interface specifications would. In fact, it can be
|
||||
better because an interface specification cannot test certain properties of a
|
||||
program. For example, the <code class="xref py py-meth docutils literal notranslate"><span class="pre">append()</span></code> method is expected to add new elements
|
||||
to the end of some internal list; an interface specification cannot test that
|
||||
your <code class="xref py py-meth docutils literal notranslate"><span class="pre">append()</span></code> implementation will actually do this correctly, but it’s
|
||||
trivial to check this property in a test suite.</p>
|
||||
<p>Writing test suites is very helpful, and you might want to design your code with
|
||||
an eye to making it easily tested. One increasingly popular technique,
|
||||
test-directed development, calls for writing parts of the test suite first,
|
||||
before you write any of the actual code. Of course Python allows you to be
|
||||
sloppy and not write test cases at all.</p>
|
||||
</div>
|
||||
<div class="section" id="why-is-there-no-goto">
|
||||
<h2><a class="toc-backref" href="#id24">Why is there no goto?</a><a class="headerlink" href="#why-is-there-no-goto" title="Permalink to this headline">¶</a></h2>
|
||||
<p>You can use exceptions to provide a “structured goto” that even works across
|
||||
function calls. Many feel that exceptions can conveniently emulate all
|
||||
reasonable uses of the “go” or “goto” constructs of C, Fortran, and other
|
||||
languages. For example:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">label</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span> <span class="k">pass</span> <span class="c1"># declare a label</span>
|
||||
|
||||
<span class="k">try</span><span class="p">:</span>
|
||||
<span class="o">...</span>
|
||||
<span class="k">if</span> <span class="n">condition</span><span class="p">:</span> <span class="k">raise</span> <span class="n">label</span><span class="p">()</span> <span class="c1"># goto label</span>
|
||||
<span class="o">...</span>
|
||||
<span class="k">except</span> <span class="n">label</span><span class="p">:</span> <span class="c1"># where to goto</span>
|
||||
<span class="k">pass</span>
|
||||
<span class="o">...</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>This doesn’t allow you to jump into the middle of a loop, but that’s usually
|
||||
considered an abuse of goto anyway. Use sparingly.</p>
|
||||
</div>
|
||||
<div class="section" id="why-can-t-raw-strings-r-strings-end-with-a-backslash">
|
||||
<h2><a class="toc-backref" href="#id25">Why can’t raw strings (r-strings) end with a backslash?</a><a class="headerlink" href="#why-can-t-raw-strings-r-strings-end-with-a-backslash" title="Permalink to this headline">¶</a></h2>
|
||||
<p>More precisely, they can’t end with an odd number of backslashes: the unpaired
|
||||
backslash at the end escapes the closing quote character, leaving an
|
||||
unterminated string.</p>
|
||||
<p>Raw strings were designed to ease creating input for processors (chiefly regular
|
||||
expression engines) that want to do their own backslash escape processing. Such
|
||||
processors consider an unmatched trailing backslash to be an error anyway, so
|
||||
raw strings disallow that. In return, they allow you to pass on the string
|
||||
quote character by escaping it with a backslash. These rules work well when
|
||||
r-strings are used for their intended purpose.</p>
|
||||
<p>If you’re trying to build Windows pathnames, note that all Windows system calls
|
||||
accept forward slashes too:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"/mydir/file.txt"</span><span class="p">)</span> <span class="c1"># works fine!</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>If you’re trying to build a pathname for a DOS command, try e.g. one of</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">dir</span> <span class="o">=</span> <span class="sa">r</span><span class="s2">"\this\is\my\dos\dir"</span> <span class="s2">"</span><span class="se">\\</span><span class="s2">"</span>
|
||||
<span class="nb">dir</span> <span class="o">=</span> <span class="sa">r</span><span class="s2">"\this\is\my\dos\dir\ "</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
|
||||
<span class="nb">dir</span> <span class="o">=</span> <span class="s2">"</span><span class="se">\\</span><span class="s2">this</span><span class="se">\\</span><span class="s2">is</span><span class="se">\\</span><span class="s2">my</span><span class="se">\\</span><span class="s2">dos</span><span class="se">\\</span><span class="s2">dir</span><span class="se">\\</span><span class="s2">"</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="why-doesn-t-python-have-a-with-statement-for-attribute-assignments">
|
||||
<h2><a class="toc-backref" href="#id26">Why doesn’t Python have a “with” statement for attribute assignments?</a><a class="headerlink" href="#why-doesn-t-python-have-a-with-statement-for-attribute-assignments" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Python has a ‘with’ statement that wraps the execution of a block, calling code
|
||||
on the entrance and exit from the block. Some language have a construct that
|
||||
looks like this:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">obj</span><span class="p">:</span>
|
||||
<span class="n">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># equivalent to obj.a = 1</span>
|
||||
<span class="n">total</span> <span class="o">=</span> <span class="n">total</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># obj.total = obj.total + 1</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>In Python, such a construct would be ambiguous.</p>
|
||||
<p>Other languages, such as Object Pascal, Delphi, and C++, use static types, so
|
||||
it’s possible to know, in an unambiguous way, what member is being assigned
|
||||
to. This is the main point of static typing – the compiler <em>always</em> knows the
|
||||
scope of every variable at compile time.</p>
|
||||
<p>Python uses dynamic types. It is impossible to know in advance which attribute
|
||||
will be referenced at runtime. Member attributes may be added or removed from
|
||||
objects on the fly. This makes it impossible to know, from a simple reading,
|
||||
what attribute is being referenced: a local one, a global one, or a member
|
||||
attribute?</p>
|
||||
<p>For instance, take the following incomplete snippet:</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">a</span><span class="p">):</span>
|
||||
<span class="k">with</span> <span class="n">a</span><span class="p">:</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The snippet assumes that “a” must have a member attribute called “x”. However,
|
||||
there is nothing in Python that tells the interpreter this. What should happen
|
||||
if “a” is, let us say, an integer? If there is a global variable named “x”,
|
||||
will it be used inside the with block? As you see, the dynamic nature of Python
|
||||
makes such choices much harder.</p>
|
||||
<p>The primary benefit of “with” and similar language features (reduction of code
|
||||
volume) can, however, easily be achieved in Python by assignment. Instead of:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">function</span><span class="p">(</span><span class="n">args</span><span class="p">)</span><span class="o">.</span><span class="n">mydict</span><span class="p">[</span><span class="n">index</span><span class="p">][</span><span class="n">index</span><span class="p">]</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="mi">21</span>
|
||||
<span class="n">function</span><span class="p">(</span><span class="n">args</span><span class="p">)</span><span class="o">.</span><span class="n">mydict</span><span class="p">[</span><span class="n">index</span><span class="p">][</span><span class="n">index</span><span class="p">]</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="mi">42</span>
|
||||
<span class="n">function</span><span class="p">(</span><span class="n">args</span><span class="p">)</span><span class="o">.</span><span class="n">mydict</span><span class="p">[</span><span class="n">index</span><span class="p">][</span><span class="n">index</span><span class="p">]</span><span class="o">.</span><span class="n">c</span> <span class="o">=</span> <span class="mi">63</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>write this:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">ref</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">args</span><span class="p">)</span><span class="o">.</span><span class="n">mydict</span><span class="p">[</span><span class="n">index</span><span class="p">][</span><span class="n">index</span><span class="p">]</span>
|
||||
<span class="n">ref</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="mi">21</span>
|
||||
<span class="n">ref</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="mi">42</span>
|
||||
<span class="n">ref</span><span class="o">.</span><span class="n">c</span> <span class="o">=</span> <span class="mi">63</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>This also has the side-effect of increasing execution speed because name
|
||||
bindings are resolved at run-time in Python, and the second version only needs
|
||||
to perform the resolution once.</p>
|
||||
</div>
|
||||
<div class="section" id="why-are-colons-required-for-the-if-while-def-class-statements">
|
||||
<h2><a class="toc-backref" href="#id27">Why are colons required for the if/while/def/class statements?</a><a class="headerlink" href="#why-are-colons-required-for-the-if-while-def-class-statements" title="Permalink to this headline">¶</a></h2>
|
||||
<p>The colon is required primarily to enhance readability (one of the results of
|
||||
the experimental ABC language). Consider this:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">a</span> <span class="o">==</span> <span class="n">b</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>versus</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="p">:</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Notice how the second one is slightly easier to read. Notice further how a
|
||||
colon sets off the example in this FAQ answer; it’s a standard usage in English.</p>
|
||||
<p>Another minor reason is that the colon makes it easier for editors with syntax
|
||||
highlighting; they can look for colons to decide when indentation needs to be
|
||||
increased instead of having to do a more elaborate parsing of the program text.</p>
|
||||
</div>
|
||||
<div class="section" id="why-does-python-allow-commas-at-the-end-of-lists-and-tuples">
|
||||
<h2><a class="toc-backref" href="#id28">Why does Python allow commas at the end of lists and tuples?</a><a class="headerlink" href="#why-does-python-allow-commas-at-the-end-of-lists-and-tuples" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Python lets you add a trailing comma at the end of lists, tuples, and
|
||||
dictionaries:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,]</span>
|
||||
<span class="p">(</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">,)</span>
|
||||
<span class="n">d</span> <span class="o">=</span> <span class="p">{</span>
|
||||
<span class="s2">"A"</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span>
|
||||
<span class="s2">"B"</span><span class="p">:</span> <span class="p">[</span><span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">],</span> <span class="c1"># last trailing comma is optional but good style</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>There are several reasons to allow this.</p>
|
||||
<p>When you have a literal value for a list, tuple, or dictionary spread across
|
||||
multiple lines, it’s easier to add more elements because you don’t have to
|
||||
remember to add a comma to the previous line. The lines can also be reordered
|
||||
without creating a syntax error.</p>
|
||||
<p>Accidentally omitting the comma can lead to errors that are hard to diagnose.
|
||||
For example:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">[</span>
|
||||
<span class="s2">"fee"</span><span class="p">,</span>
|
||||
<span class="s2">"fie"</span>
|
||||
<span class="s2">"foo"</span><span class="p">,</span>
|
||||
<span class="s2">"fum"</span>
|
||||
<span class="p">]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>This list looks like it has four elements, but it actually contains three:
|
||||
“fee”, “fiefoo” and “fum”. Always adding the comma avoids this source of error.</p>
|
||||
<p>Allowing the trailing comma may also make programmatic code generation easier.</p>
|
||||
</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="#">Design and History FAQ</a><ul>
|
||||
<li><a class="reference internal" href="#why-does-python-use-indentation-for-grouping-of-statements">Why does Python use indentation for grouping of statements?</a></li>
|
||||
<li><a class="reference internal" href="#why-am-i-getting-strange-results-with-simple-arithmetic-operations">Why am I getting strange results with simple arithmetic operations?</a></li>
|
||||
<li><a class="reference internal" href="#why-are-floating-point-calculations-so-inaccurate">Why are floating-point calculations so inaccurate?</a></li>
|
||||
<li><a class="reference internal" href="#why-are-python-strings-immutable">Why are Python strings immutable?</a></li>
|
||||
<li><a class="reference internal" href="#why-must-self-be-used-explicitly-in-method-definitions-and-calls">Why must ‘self’ be used explicitly in method definitions and calls?</a></li>
|
||||
<li><a class="reference internal" href="#why-can-t-i-use-an-assignment-in-an-expression">Why can’t I use an assignment in an expression?</a></li>
|
||||
<li><a class="reference internal" href="#why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list">Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?</a></li>
|
||||
<li><a class="reference internal" href="#why-is-join-a-string-method-instead-of-a-list-or-tuple-method">Why is join() a string method instead of a list or tuple method?</a></li>
|
||||
<li><a class="reference internal" href="#how-fast-are-exceptions">How fast are exceptions?</a></li>
|
||||
<li><a class="reference internal" href="#why-isn-t-there-a-switch-or-case-statement-in-python">Why isn’t there a switch or case statement in Python?</a></li>
|
||||
<li><a class="reference internal" href="#can-t-you-emulate-threads-in-the-interpreter-instead-of-relying-on-an-os-specific-thread-implementation">Can’t you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?</a></li>
|
||||
<li><a class="reference internal" href="#why-can-t-lambda-expressions-contain-statements">Why can’t lambda expressions contain statements?</a></li>
|
||||
<li><a class="reference internal" href="#can-python-be-compiled-to-machine-code-c-or-some-other-language">Can Python be compiled to machine code, C or some other language?</a></li>
|
||||
<li><a class="reference internal" href="#how-does-python-manage-memory">How does Python manage memory?</a></li>
|
||||
<li><a class="reference internal" href="#why-doesn-t-cpython-use-a-more-traditional-garbage-collection-scheme">Why doesn’t CPython use a more traditional garbage collection scheme?</a></li>
|
||||
<li><a class="reference internal" href="#why-isn-t-all-memory-freed-when-cpython-exits">Why isn’t all memory freed when CPython exits?</a></li>
|
||||
<li><a class="reference internal" href="#why-are-there-separate-tuple-and-list-data-types">Why are there separate tuple and list data types?</a></li>
|
||||
<li><a class="reference internal" href="#how-are-lists-implemented-in-cpython">How are lists implemented in CPython?</a></li>
|
||||
<li><a class="reference internal" href="#how-are-dictionaries-implemented-in-cpython">How are dictionaries implemented in CPython?</a></li>
|
||||
<li><a class="reference internal" href="#why-must-dictionary-keys-be-immutable">Why must dictionary keys be immutable?</a></li>
|
||||
<li><a class="reference internal" href="#why-doesn-t-list-sort-return-the-sorted-list">Why doesn’t list.sort() return the sorted list?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-you-specify-and-enforce-an-interface-spec-in-python">How do you specify and enforce an interface spec in Python?</a></li>
|
||||
<li><a class="reference internal" href="#why-is-there-no-goto">Why is there no goto?</a></li>
|
||||
<li><a class="reference internal" href="#why-can-t-raw-strings-r-strings-end-with-a-backslash">Why can’t raw strings (r-strings) end with a backslash?</a></li>
|
||||
<li><a class="reference internal" href="#why-doesn-t-python-have-a-with-statement-for-attribute-assignments">Why doesn’t Python have a “with” statement for attribute assignments?</a></li>
|
||||
<li><a class="reference internal" href="#why-are-colons-required-for-the-if-while-def-class-statements">Why are colons required for the if/while/def/class statements?</a></li>
|
||||
<li><a class="reference internal" href="#why-does-python-allow-commas-at-the-end-of-lists-and-tuples">Why does Python allow commas at the end of lists and tuples?</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>Previous topic</h4>
|
||||
<p class="topless"><a href="programming.html"
|
||||
title="previous chapter">Programming FAQ</a></p>
|
||||
<h4>Next topic</h4>
|
||||
<p class="topless"><a href="library.html"
|
||||
title="next chapter">Library and Extension 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/design.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="library.html" title="Library and Extension FAQ"
|
||||
>next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="programming.html" title="Programming 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>
|
601
python-3.7.4-docs-html/faq/extending.html
Normal file
601
python-3.7.4-docs-html/faq/extending.html
Normal file
@@ -0,0 +1,601 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Extending/Embedding 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="Python on Windows FAQ" href="windows.html" />
|
||||
<link rel="prev" title="Library and Extension FAQ" href="library.html" />
|
||||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/faq/extending.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="windows.html" title="Python on Windows FAQ"
|
||||
accesskey="N">next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="library.html" title="Library and Extension 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="extending-embedding-faq">
|
||||
<h1><a class="toc-backref" href="#id2">Extending/Embedding FAQ</a><a class="headerlink" href="#extending-embedding-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="#extending-embedding-faq" id="id2">Extending/Embedding FAQ</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#can-i-create-my-own-functions-in-c" id="id3">Can I create my own functions in C?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#id1" id="id4">Can I create my own functions in C++?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#writing-c-is-hard-are-there-any-alternatives" id="id5">Writing C is hard; are there any alternatives?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-can-i-execute-arbitrary-python-statements-from-c" id="id6">How can I execute arbitrary Python statements from C?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-can-i-evaluate-an-arbitrary-python-expression-from-c" id="id7">How can I evaluate an arbitrary Python expression from C?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-extract-c-values-from-a-python-object" id="id8">How do I extract C values from a Python object?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-use-py-buildvalue-to-create-a-tuple-of-arbitrary-length" id="id9">How do I use Py_BuildValue() to create a tuple of arbitrary length?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-call-an-object-s-method-from-c" id="id10">How do I call an object’s method from C?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-catch-the-output-from-pyerr-print-or-anything-that-prints-to-stdout-stderr" id="id11">How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-access-a-module-written-in-python-from-c" id="id12">How do I access a module written in Python from C?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-interface-to-c-objects-from-python" id="id13">How do I interface to C++ objects from Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#i-added-a-module-using-the-setup-file-and-the-make-fails-why" id="id14">I added a module using the Setup file and the make fails; why?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-debug-an-extension" id="id15">How do I debug an extension?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#i-want-to-compile-a-python-module-on-my-linux-system-but-some-files-are-missing-why" id="id16">I want to compile a Python module on my Linux system, but some files are missing. Why?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-tell-incomplete-input-from-invalid-input" id="id17">How do I tell “incomplete input” from “invalid input”?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-find-undefined-g-symbols-builtin-new-or-pure-virtual" id="id18">How do I find undefined g++ symbols __builtin_new or __pure_virtual?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#can-i-create-an-object-class-with-some-methods-implemented-in-c-and-others-in-python-e-g-through-inheritance" id="id19">Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section" id="can-i-create-my-own-functions-in-c">
|
||||
<h2><a class="toc-backref" href="#id3">Can I create my own functions in C?</a><a class="headerlink" href="#can-i-create-my-own-functions-in-c" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Yes, you can create built-in modules containing functions, variables, exceptions
|
||||
and even new types in C. This is explained in the document
|
||||
<a class="reference internal" href="../extending/index.html#extending-index"><span class="std std-ref">Extending and Embedding the Python Interpreter</span></a>.</p>
|
||||
<p>Most intermediate or advanced Python books will also cover this topic.</p>
|
||||
</div>
|
||||
<div class="section" id="id1">
|
||||
<h2><a class="toc-backref" href="#id4">Can I create my own functions in C++?</a><a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Yes, using the C compatibility features found in C++. Place <code class="docutils literal notranslate"><span class="pre">extern</span> <span class="pre">"C"</span> <span class="pre">{</span>
|
||||
<span class="pre">...</span> <span class="pre">}</span></code> around the Python include files and put <code class="docutils literal notranslate"><span class="pre">extern</span> <span class="pre">"C"</span></code> before each
|
||||
function that is going to be called by the Python interpreter. Global or static
|
||||
C++ objects with constructors are probably not a good idea.</p>
|
||||
</div>
|
||||
<div class="section" id="writing-c-is-hard-are-there-any-alternatives">
|
||||
<span id="c-wrapper-software"></span><h2><a class="toc-backref" href="#id5">Writing C is hard; are there any alternatives?</a><a class="headerlink" href="#writing-c-is-hard-are-there-any-alternatives" title="Permalink to this headline">¶</a></h2>
|
||||
<p>There are a number of alternatives to writing your own C extensions, depending
|
||||
on what you’re trying to do.</p>
|
||||
<p><a class="reference external" href="http://cython.org">Cython</a> and its relative <a class="reference external" href="https://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/">Pyrex</a> are compilers
|
||||
that accept a slightly modified form of Python and generate the corresponding
|
||||
C code. Cython and Pyrex make it possible to write an extension without having
|
||||
to learn Python’s C API.</p>
|
||||
<p>If you need to interface to some C or C++ library for which no Python extension
|
||||
currently exists, you can try wrapping the library’s data types and functions
|
||||
with a tool such as <a class="reference external" href="http://www.swig.org">SWIG</a>. <a class="reference external" href="https://riverbankcomputing.com/software/sip/intro">SIP</a>, <a class="reference external" href="http://cxx.sourceforge.net/">CXX</a> <a class="reference external" href="http://www.boost.org/libs/python/doc/index.html">Boost</a>, or <a class="reference external" href="https://github.com/scipy/weave">Weave</a> are also
|
||||
alternatives for wrapping C++ libraries.</p>
|
||||
</div>
|
||||
<div class="section" id="how-can-i-execute-arbitrary-python-statements-from-c">
|
||||
<h2><a class="toc-backref" href="#id6">How can I execute arbitrary Python statements from C?</a><a class="headerlink" href="#how-can-i-execute-arbitrary-python-statements-from-c" title="Permalink to this headline">¶</a></h2>
|
||||
<p>The highest-level function to do this is <a class="reference internal" href="../c-api/veryhigh.html#c.PyRun_SimpleString" title="PyRun_SimpleString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyRun_SimpleString()</span></code></a> which takes
|
||||
a single string argument to be executed in the context of the module
|
||||
<code class="docutils literal notranslate"><span class="pre">__main__</span></code> and returns <code class="docutils literal notranslate"><span class="pre">0</span></code> for success and <code class="docutils literal notranslate"><span class="pre">-1</span></code> when an exception occurred
|
||||
(including <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a>). If you want more control, use
|
||||
<a class="reference internal" href="../c-api/veryhigh.html#c.PyRun_String" title="PyRun_String"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyRun_String()</span></code></a>; see the source for <a class="reference internal" href="../c-api/veryhigh.html#c.PyRun_SimpleString" title="PyRun_SimpleString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyRun_SimpleString()</span></code></a> in
|
||||
<code class="docutils literal notranslate"><span class="pre">Python/pythonrun.c</span></code>.</p>
|
||||
</div>
|
||||
<div class="section" id="how-can-i-evaluate-an-arbitrary-python-expression-from-c">
|
||||
<h2><a class="toc-backref" href="#id7">How can I evaluate an arbitrary Python expression from C?</a><a class="headerlink" href="#how-can-i-evaluate-an-arbitrary-python-expression-from-c" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Call the function <a class="reference internal" href="../c-api/veryhigh.html#c.PyRun_String" title="PyRun_String"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyRun_String()</span></code></a> from the previous question with the
|
||||
start symbol <a class="reference internal" href="../c-api/veryhigh.html#c.Py_eval_input" title="Py_eval_input"><code class="xref c c-data docutils literal notranslate"><span class="pre">Py_eval_input</span></code></a>; it parses an expression, evaluates it and
|
||||
returns its value.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-extract-c-values-from-a-python-object">
|
||||
<h2><a class="toc-backref" href="#id8">How do I extract C values from a Python object?</a><a class="headerlink" href="#how-do-i-extract-c-values-from-a-python-object" title="Permalink to this headline">¶</a></h2>
|
||||
<p>That depends on the object’s type. If it’s a tuple, <a class="reference internal" href="../c-api/tuple.html#c.PyTuple_Size" title="PyTuple_Size"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_Size()</span></code></a>
|
||||
returns its length and <a class="reference internal" href="../c-api/tuple.html#c.PyTuple_GetItem" title="PyTuple_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_GetItem()</span></code></a> returns the item at a specified
|
||||
index. Lists have similar functions, <code class="xref c c-func docutils literal notranslate"><span class="pre">PyListSize()</span></code> and
|
||||
<a class="reference internal" href="../c-api/list.html#c.PyList_GetItem" title="PyList_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_GetItem()</span></code></a>.</p>
|
||||
<p>For bytes, <a class="reference internal" href="../c-api/bytes.html#c.PyBytes_Size" title="PyBytes_Size"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBytes_Size()</span></code></a> returns its length and
|
||||
<a class="reference internal" href="../c-api/bytes.html#c.PyBytes_AsStringAndSize" title="PyBytes_AsStringAndSize"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBytes_AsStringAndSize()</span></code></a> provides a pointer to its value and its
|
||||
length. Note that Python bytes objects may contain null bytes so C’s
|
||||
<code class="xref c c-func docutils literal notranslate"><span class="pre">strlen()</span></code> should not be used.</p>
|
||||
<p>To test the type of an object, first make sure it isn’t <em>NULL</em>, and then use
|
||||
<a class="reference internal" href="../c-api/bytes.html#c.PyBytes_Check" title="PyBytes_Check"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBytes_Check()</span></code></a>, <a class="reference internal" href="../c-api/tuple.html#c.PyTuple_Check" title="PyTuple_Check"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_Check()</span></code></a>, <a class="reference internal" href="../c-api/list.html#c.PyList_Check" title="PyList_Check"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_Check()</span></code></a>, etc.</p>
|
||||
<p>There is also a high-level API to Python objects which is provided by the
|
||||
so-called ‘abstract’ interface – read <code class="docutils literal notranslate"><span class="pre">Include/abstract.h</span></code> for further
|
||||
details. It allows interfacing with any kind of Python sequence using calls
|
||||
like <a class="reference internal" href="../c-api/sequence.html#c.PySequence_Length" title="PySequence_Length"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_Length()</span></code></a>, <a class="reference internal" href="../c-api/sequence.html#c.PySequence_GetItem" title="PySequence_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_GetItem()</span></code></a>, etc. as well
|
||||
as many other useful protocols such as numbers (<a class="reference internal" href="../c-api/number.html#c.PyNumber_Index" title="PyNumber_Index"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_Index()</span></code></a> et
|
||||
al.) and mappings in the PyMapping APIs.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-use-py-buildvalue-to-create-a-tuple-of-arbitrary-length">
|
||||
<h2><a class="toc-backref" href="#id9">How do I use Py_BuildValue() to create a tuple of arbitrary length?</a><a class="headerlink" href="#how-do-i-use-py-buildvalue-to-create-a-tuple-of-arbitrary-length" title="Permalink to this headline">¶</a></h2>
|
||||
<p>You can’t. Use <a class="reference internal" href="../c-api/tuple.html#c.PyTuple_Pack" title="PyTuple_Pack"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_Pack()</span></code></a> instead.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-call-an-object-s-method-from-c">
|
||||
<h2><a class="toc-backref" href="#id10">How do I call an object’s method from C?</a><a class="headerlink" href="#how-do-i-call-an-object-s-method-from-c" title="Permalink to this headline">¶</a></h2>
|
||||
<p>The <a class="reference internal" href="../c-api/object.html#c.PyObject_CallMethod" title="PyObject_CallMethod"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallMethod()</span></code></a> function can be used to call an arbitrary
|
||||
method of an object. The parameters are the object, the name of the method to
|
||||
call, a format string like that used with <a class="reference internal" href="../c-api/arg.html#c.Py_BuildValue" title="Py_BuildValue"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_BuildValue()</span></code></a>, and the
|
||||
argument values:</p>
|
||||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span> <span class="o">*</span>
|
||||
<span class="nf">PyObject_CallMethod</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">object</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">method_name</span><span class="p">,</span>
|
||||
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">arg_format</span><span class="p">,</span> <span class="p">...);</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>This works for any object that has methods – whether built-in or user-defined.
|
||||
You are responsible for eventually <a class="reference internal" href="../c-api/refcounting.html#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a>’ing the return value.</p>
|
||||
<p>To call, e.g., a file object’s “seek” method with arguments 10, 0 (assuming the
|
||||
file object pointer is “f”):</p>
|
||||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">res</span> <span class="o">=</span> <span class="n">PyObject_CallMethod</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s">"seek"</span><span class="p">,</span> <span class="s">"(ii)"</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
|
||||
<span class="k">if</span> <span class="p">(</span><span class="n">res</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
|
||||
<span class="p">...</span> <span class="n">an</span> <span class="n">exception</span> <span class="n">occurred</span> <span class="p">...</span>
|
||||
<span class="p">}</span>
|
||||
<span class="k">else</span> <span class="p">{</span>
|
||||
<span class="n">Py_DECREF</span><span class="p">(</span><span class="n">res</span><span class="p">);</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Note that since <a class="reference internal" href="../c-api/object.html#c.PyObject_CallObject" title="PyObject_CallObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallObject()</span></code></a> <em>always</em> wants a tuple for the
|
||||
argument list, to call a function without arguments, pass “()” for the format,
|
||||
and to call a function with one argument, surround the argument in parentheses,
|
||||
e.g. “(i)”.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-catch-the-output-from-pyerr-print-or-anything-that-prints-to-stdout-stderr">
|
||||
<h2><a class="toc-backref" href="#id11">How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)?</a><a class="headerlink" href="#how-do-i-catch-the-output-from-pyerr-print-or-anything-that-prints-to-stdout-stderr" title="Permalink to this headline">¶</a></h2>
|
||||
<p>In Python code, define an object that supports the <code class="docutils literal notranslate"><span class="pre">write()</span></code> method. Assign
|
||||
this object to <a class="reference internal" href="../library/sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a> and <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>. Call print_error, or
|
||||
just allow the standard traceback mechanism to work. Then, the output will go
|
||||
wherever your <code class="docutils literal notranslate"><span class="pre">write()</span></code> method sends it.</p>
|
||||
<p>The easiest way to do this is to use the <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> class:</p>
|
||||
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">io</span><span class="o">,</span> <span class="nn">sys</span>
|
||||
<span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</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="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">)</span>
|
||||
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="s1">'hello world!'</span><span class="p">)</span>
|
||||
<span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">getvalue</span><span class="p">())</span>
|
||||
<span class="go">foo</span>
|
||||
<span class="go">hello world!</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>A custom object to do the same would look like this:</p>
|
||||
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">io</span><span class="o">,</span> <span class="nn">sys</span>
|
||||
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">StdoutCatcher</span><span class="p">(</span><span class="n">io</span><span class="o">.</span><span class="n">TextIOBase</span><span class="p">):</span>
|
||||
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="gp">... </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">stuff</span><span class="p">):</span>
|
||||
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">stuff</span><span class="p">)</span>
|
||||
<span class="gp">...</span>
|
||||
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sys</span>
|
||||
<span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span> <span class="o">=</span> <span class="n">StdoutCatcher</span><span class="p">()</span>
|
||||
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">)</span>
|
||||
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="s1">'hello world!'</span><span class="p">)</span>
|
||||
<span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">data</span><span class="p">))</span>
|
||||
<span class="go">foo</span>
|
||||
<span class="go">hello world!</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-access-a-module-written-in-python-from-c">
|
||||
<h2><a class="toc-backref" href="#id12">How do I access a module written in Python from C?</a><a class="headerlink" href="#how-do-i-access-a-module-written-in-python-from-c" title="Permalink to this headline">¶</a></h2>
|
||||
<p>You can get a pointer to the module object as follows:</p>
|
||||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">module</span> <span class="o">=</span> <span class="n">PyImport_ImportModule</span><span class="p">(</span><span class="s">"<modulename>"</span><span class="p">);</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>If the module hasn’t been imported yet (i.e. it is not yet present 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>), this initializes the module; otherwise it simply returns
|
||||
the value of <code class="docutils literal notranslate"><span class="pre">sys.modules["<modulename>"]</span></code>. Note that it doesn’t enter the
|
||||
module into any namespace – it only ensures it has been initialized and is
|
||||
stored 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>
|
||||
<p>You can then access the module’s attributes (i.e. any name defined in the
|
||||
module) as follows:</p>
|
||||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">attr</span> <span class="o">=</span> <span class="n">PyObject_GetAttrString</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="s">"<attrname>"</span><span class="p">);</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Calling <a class="reference internal" href="../c-api/object.html#c.PyObject_SetAttrString" title="PyObject_SetAttrString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_SetAttrString()</span></code></a> to assign to variables in the module
|
||||
also works.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-interface-to-c-objects-from-python">
|
||||
<h2><a class="toc-backref" href="#id13">How do I interface to C++ objects from Python?</a><a class="headerlink" href="#how-do-i-interface-to-c-objects-from-python" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Depending on your requirements, there are many approaches. To do this manually,
|
||||
begin by reading <a class="reference internal" href="../extending/index.html#extending-index"><span class="std std-ref">the “Extending and Embedding” document</span></a>. Realize that for the Python run-time system, there isn’t a
|
||||
whole lot of difference between C and C++ – so the strategy of building a new
|
||||
Python type around a C structure (pointer) type will also work for C++ objects.</p>
|
||||
<p>For C++ libraries, see <a class="reference internal" href="#c-wrapper-software"><span class="std std-ref">Writing C is hard; are there any alternatives?</span></a>.</p>
|
||||
</div>
|
||||
<div class="section" id="i-added-a-module-using-the-setup-file-and-the-make-fails-why">
|
||||
<h2><a class="toc-backref" href="#id14">I added a module using the Setup file and the make fails; why?</a><a class="headerlink" href="#i-added-a-module-using-the-setup-file-and-the-make-fails-why" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Setup must end in a newline, if there is no newline there, the build process
|
||||
fails. (Fixing this requires some ugly shell script hackery, and this bug is so
|
||||
minor that it doesn’t seem worth the effort.)</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-debug-an-extension">
|
||||
<h2><a class="toc-backref" href="#id15">How do I debug an extension?</a><a class="headerlink" href="#how-do-i-debug-an-extension" title="Permalink to this headline">¶</a></h2>
|
||||
<p>When using GDB with dynamically loaded extensions, you can’t set a breakpoint in
|
||||
your extension until your extension is loaded.</p>
|
||||
<p>In your <code class="docutils literal notranslate"><span class="pre">.gdbinit</span></code> file (or interactively), add the command:</p>
|
||||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>br _PyImport_LoadDynamicModule
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Then, when you run GDB:</p>
|
||||
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> gdb /local/bin/python
|
||||
<span class="go">gdb) run myscript.py</span>
|
||||
<span class="go">gdb) continue # repeat until your extension is loaded</span>
|
||||
<span class="go">gdb) finish # so that your extension is loaded</span>
|
||||
<span class="go">gdb) br myfunction.c:50</span>
|
||||
<span class="go">gdb) continue</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="i-want-to-compile-a-python-module-on-my-linux-system-but-some-files-are-missing-why">
|
||||
<h2><a class="toc-backref" href="#id16">I want to compile a Python module on my Linux system, but some files are missing. Why?</a><a class="headerlink" href="#i-want-to-compile-a-python-module-on-my-linux-system-but-some-files-are-missing-why" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Most packaged versions of Python don’t include the
|
||||
<code class="file docutils literal notranslate"><span class="pre">/usr/lib/python2.</span><em><span class="pre">x</span></em><span class="pre">/config/</span></code> directory, which contains various files
|
||||
required for compiling Python extensions.</p>
|
||||
<p>For Red Hat, install the python-devel RPM to get the necessary files.</p>
|
||||
<p>For Debian, run <code class="docutils literal notranslate"><span class="pre">apt-get</span> <span class="pre">install</span> <span class="pre">python-dev</span></code>.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-tell-incomplete-input-from-invalid-input">
|
||||
<h2><a class="toc-backref" href="#id17">How do I tell “incomplete input” from “invalid input”?</a><a class="headerlink" href="#how-do-i-tell-incomplete-input-from-invalid-input" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Sometimes you want to emulate the Python interactive interpreter’s behavior,
|
||||
where it gives you a continuation prompt when the input is incomplete (e.g. you
|
||||
typed the start of an “if” statement or you didn’t close your parentheses or
|
||||
triple string quotes), but it gives you a syntax error message immediately when
|
||||
the input is invalid.</p>
|
||||
<p>In Python you can use the <a class="reference internal" href="../library/codeop.html#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code></a> module, which approximates the parser’s
|
||||
behavior sufficiently. IDLE uses this, for example.</p>
|
||||
<p>The easiest way to do it in C is to call <a class="reference internal" href="../c-api/veryhigh.html#c.PyRun_InteractiveLoop" title="PyRun_InteractiveLoop"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyRun_InteractiveLoop()</span></code></a> (perhaps
|
||||
in a separate thread) and let the Python interpreter handle the input for
|
||||
you. You can also set the <a class="reference internal" href="../c-api/veryhigh.html#c.PyOS_ReadlineFunctionPointer" title="PyOS_ReadlineFunctionPointer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ReadlineFunctionPointer()</span></code></a> to point at your
|
||||
custom input function. See <code class="docutils literal notranslate"><span class="pre">Modules/readline.c</span></code> and <code class="docutils literal notranslate"><span class="pre">Parser/myreadline.c</span></code>
|
||||
for more hints.</p>
|
||||
<p>However sometimes you have to run the embedded Python interpreter in the same
|
||||
thread as your rest application and you can’t allow the
|
||||
<a class="reference internal" href="../c-api/veryhigh.html#c.PyRun_InteractiveLoop" title="PyRun_InteractiveLoop"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyRun_InteractiveLoop()</span></code></a> to stop while waiting for user input. The one
|
||||
solution then is to call <code class="xref c c-func docutils literal notranslate"><span class="pre">PyParser_ParseString()</span></code> and test for <code class="docutils literal notranslate"><span class="pre">e.error</span></code>
|
||||
equal to <code class="docutils literal notranslate"><span class="pre">E_EOF</span></code>, which means the input is incomplete. Here’s a sample code
|
||||
fragment, untested, inspired by code from Alex Farber:</p>
|
||||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define PY_SSIZE_T_CLEAN</span>
|
||||
<span class="cp">#include</span> <span class="cpf"><Python.h></span><span class="cp"></span>
|
||||
<span class="cp">#include</span> <span class="cpf"><node.h></span><span class="cp"></span>
|
||||
<span class="cp">#include</span> <span class="cpf"><errcode.h></span><span class="cp"></span>
|
||||
<span class="cp">#include</span> <span class="cpf"><grammar.h></span><span class="cp"></span>
|
||||
<span class="cp">#include</span> <span class="cpf"><parsetok.h></span><span class="cp"></span>
|
||||
<span class="cp">#include</span> <span class="cpf"><compile.h></span><span class="cp"></span>
|
||||
|
||||
<span class="kt">int</span> <span class="nf">testcomplete</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">code</span><span class="p">)</span>
|
||||
<span class="cm">/* code should end in \n */</span>
|
||||
<span class="cm">/* return -1 for error, 0 for incomplete, 1 for complete */</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">node</span> <span class="o">*</span><span class="n">n</span><span class="p">;</span>
|
||||
<span class="n">perrdetail</span> <span class="n">e</span><span class="p">;</span>
|
||||
|
||||
<span class="n">n</span> <span class="o">=</span> <span class="n">PyParser_ParseString</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="o">&</span><span class="n">_PyParser_Grammar</span><span class="p">,</span>
|
||||
<span class="n">Py_file_input</span><span class="p">,</span> <span class="o">&</span><span class="n">e</span><span class="p">);</span>
|
||||
<span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
|
||||
<span class="k">if</span> <span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="n">error</span> <span class="o">==</span> <span class="n">E_EOF</span><span class="p">)</span>
|
||||
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
|
||||
<span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
|
||||
<span class="n">PyNode_Free</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
|
||||
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Another solution is trying to compile the received string with
|
||||
<a class="reference internal" href="../c-api/veryhigh.html#c.Py_CompileString" title="Py_CompileString"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_CompileString()</span></code></a>. If it compiles without errors, try to execute the
|
||||
returned code object by calling <a class="reference internal" href="../c-api/veryhigh.html#c.PyEval_EvalCode" title="PyEval_EvalCode"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_EvalCode()</span></code></a>. Otherwise save the
|
||||
input for later. If the compilation fails, find out if it’s an error or just
|
||||
more input is required - by extracting the message string from the exception
|
||||
tuple and comparing it to the string “unexpected EOF while parsing”. Here is a
|
||||
complete example using the GNU readline library (you may want to ignore
|
||||
<strong>SIGINT</strong> while calling readline()):</p>
|
||||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf"><stdio.h></span><span class="cp"></span>
|
||||
<span class="cp">#include</span> <span class="cpf"><readline.h></span><span class="cp"></span>
|
||||
|
||||
<span class="cp">#define PY_SSIZE_T_CLEAN</span>
|
||||
<span class="cp">#include</span> <span class="cpf"><Python.h></span><span class="cp"></span>
|
||||
<span class="cp">#include</span> <span class="cpf"><object.h></span><span class="cp"></span>
|
||||
<span class="cp">#include</span> <span class="cpf"><compile.h></span><span class="cp"></span>
|
||||
<span class="cp">#include</span> <span class="cpf"><eval.h></span><span class="cp"></span>
|
||||
|
||||
<span class="kt">int</span> <span class="nf">main</span> <span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span>
|
||||
<span class="p">{</span>
|
||||
<span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">done</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="cm">/* lengths of line, code */</span>
|
||||
<span class="kt">char</span> <span class="n">ps1</span><span class="p">[]</span> <span class="o">=</span> <span class="s">">>> "</span><span class="p">;</span>
|
||||
<span class="kt">char</span> <span class="n">ps2</span><span class="p">[]</span> <span class="o">=</span> <span class="s">"... "</span><span class="p">;</span>
|
||||
<span class="kt">char</span> <span class="o">*</span><span class="n">prompt</span> <span class="o">=</span> <span class="n">ps1</span><span class="p">;</span>
|
||||
<span class="kt">char</span> <span class="o">*</span><span class="n">msg</span><span class="p">,</span> <span class="o">*</span><span class="n">line</span><span class="p">,</span> <span class="o">*</span><span class="n">code</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
|
||||
<span class="n">PyObject</span> <span class="o">*</span><span class="n">src</span><span class="p">,</span> <span class="o">*</span><span class="n">glb</span><span class="p">,</span> <span class="o">*</span><span class="n">loc</span><span class="p">;</span>
|
||||
<span class="n">PyObject</span> <span class="o">*</span><span class="n">exc</span><span class="p">,</span> <span class="o">*</span><span class="n">val</span><span class="p">,</span> <span class="o">*</span><span class="n">trb</span><span class="p">,</span> <span class="o">*</span><span class="n">obj</span><span class="p">,</span> <span class="o">*</span><span class="n">dum</span><span class="p">;</span>
|
||||
|
||||
<span class="n">Py_Initialize</span> <span class="p">();</span>
|
||||
<span class="n">loc</span> <span class="o">=</span> <span class="n">PyDict_New</span> <span class="p">();</span>
|
||||
<span class="n">glb</span> <span class="o">=</span> <span class="n">PyDict_New</span> <span class="p">();</span>
|
||||
<span class="n">PyDict_SetItemString</span> <span class="p">(</span><span class="n">glb</span><span class="p">,</span> <span class="s">"__builtins__"</span><span class="p">,</span> <span class="n">PyEval_GetBuiltins</span> <span class="p">());</span>
|
||||
|
||||
<span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="n">done</span><span class="p">)</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">line</span> <span class="o">=</span> <span class="n">readline</span> <span class="p">(</span><span class="n">prompt</span><span class="p">);</span>
|
||||
|
||||
<span class="k">if</span> <span class="p">(</span><span class="nb">NULL</span> <span class="o">==</span> <span class="n">line</span><span class="p">)</span> <span class="cm">/* Ctrl-D pressed */</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">done</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
<span class="k">else</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">i</span> <span class="o">=</span> <span class="n">strlen</span> <span class="p">(</span><span class="n">line</span><span class="p">);</span>
|
||||
|
||||
<span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span>
|
||||
<span class="n">add_history</span> <span class="p">(</span><span class="n">line</span><span class="p">);</span> <span class="cm">/* save non-empty lines */</span>
|
||||
|
||||
<span class="k">if</span> <span class="p">(</span><span class="nb">NULL</span> <span class="o">==</span> <span class="n">code</span><span class="p">)</span> <span class="cm">/* nothing in code yet */</span>
|
||||
<span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
|
||||
<span class="k">else</span>
|
||||
<span class="n">j</span> <span class="o">=</span> <span class="n">strlen</span> <span class="p">(</span><span class="n">code</span><span class="p">);</span>
|
||||
|
||||
<span class="n">code</span> <span class="o">=</span> <span class="n">realloc</span> <span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">i</span> <span class="o">+</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">2</span><span class="p">);</span>
|
||||
<span class="k">if</span> <span class="p">(</span><span class="nb">NULL</span> <span class="o">==</span> <span class="n">code</span><span class="p">)</span> <span class="cm">/* out of memory */</span>
|
||||
<span class="n">exit</span> <span class="p">(</span><span class="mi">1</span><span class="p">);</span>
|
||||
|
||||
<span class="k">if</span> <span class="p">(</span><span class="mi">0</span> <span class="o">==</span> <span class="n">j</span><span class="p">)</span> <span class="cm">/* code was empty, so */</span>
|
||||
<span class="n">code</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="sc">'\0'</span><span class="p">;</span> <span class="cm">/* keep strncat happy */</span>
|
||||
|
||||
<span class="n">strncat</span> <span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">line</span><span class="p">,</span> <span class="n">i</span><span class="p">);</span> <span class="cm">/* append line to code */</span>
|
||||
<span class="n">code</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="sc">'\n'</span><span class="p">;</span> <span class="cm">/* append '\n' to code */</span>
|
||||
<span class="n">code</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="sc">'\0'</span><span class="p">;</span>
|
||||
|
||||
<span class="n">src</span> <span class="o">=</span> <span class="n">Py_CompileString</span> <span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="s">"<stdin>"</span><span class="p">,</span> <span class="n">Py_single_input</span><span class="p">);</span>
|
||||
|
||||
<span class="k">if</span> <span class="p">(</span><span class="nb">NULL</span> <span class="o">!=</span> <span class="n">src</span><span class="p">)</span> <span class="cm">/* compiled just fine - */</span>
|
||||
<span class="p">{</span>
|
||||
<span class="k">if</span> <span class="p">(</span><span class="n">ps1</span> <span class="o">==</span> <span class="n">prompt</span> <span class="o">||</span> <span class="cm">/* ">>> " or */</span>
|
||||
<span class="sc">'\n'</span> <span class="o">==</span> <span class="n">code</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="n">j</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span> <span class="cm">/* "... " and double '\n' */</span>
|
||||
<span class="p">{</span> <span class="cm">/* so execute it */</span>
|
||||
<span class="n">dum</span> <span class="o">=</span> <span class="n">PyEval_EvalCode</span> <span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">glb</span><span class="p">,</span> <span class="n">loc</span><span class="p">);</span>
|
||||
<span class="n">Py_XDECREF</span> <span class="p">(</span><span class="n">dum</span><span class="p">);</span>
|
||||
<span class="n">Py_XDECREF</span> <span class="p">(</span><span class="n">src</span><span class="p">);</span>
|
||||
<span class="n">free</span> <span class="p">(</span><span class="n">code</span><span class="p">);</span>
|
||||
<span class="n">code</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
|
||||
<span class="k">if</span> <span class="p">(</span><span class="n">PyErr_Occurred</span> <span class="p">())</span>
|
||||
<span class="n">PyErr_Print</span> <span class="p">();</span>
|
||||
<span class="n">prompt</span> <span class="o">=</span> <span class="n">ps1</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">}</span> <span class="cm">/* syntax error or E_EOF? */</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyErr_ExceptionMatches</span> <span class="p">(</span><span class="n">PyExc_SyntaxError</span><span class="p">))</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">PyErr_Fetch</span> <span class="p">(</span><span class="o">&</span><span class="n">exc</span><span class="p">,</span> <span class="o">&</span><span class="n">val</span><span class="p">,</span> <span class="o">&</span><span class="n">trb</span><span class="p">);</span> <span class="cm">/* clears exception! */</span>
|
||||
|
||||
<span class="k">if</span> <span class="p">(</span><span class="n">PyArg_ParseTuple</span> <span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="s">"sO"</span><span class="p">,</span> <span class="o">&</span><span class="n">msg</span><span class="p">,</span> <span class="o">&</span><span class="n">obj</span><span class="p">)</span> <span class="o">&&</span>
|
||||
<span class="o">!</span><span class="n">strcmp</span> <span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="s">"unexpected EOF while parsing"</span><span class="p">))</span> <span class="cm">/* E_EOF */</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">Py_XDECREF</span> <span class="p">(</span><span class="n">exc</span><span class="p">);</span>
|
||||
<span class="n">Py_XDECREF</span> <span class="p">(</span><span class="n">val</span><span class="p">);</span>
|
||||
<span class="n">Py_XDECREF</span> <span class="p">(</span><span class="n">trb</span><span class="p">);</span>
|
||||
<span class="n">prompt</span> <span class="o">=</span> <span class="n">ps2</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
<span class="k">else</span> <span class="cm">/* some other syntax error */</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">PyErr_Restore</span> <span class="p">(</span><span class="n">exc</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">trb</span><span class="p">);</span>
|
||||
<span class="n">PyErr_Print</span> <span class="p">();</span>
|
||||
<span class="n">free</span> <span class="p">(</span><span class="n">code</span><span class="p">);</span>
|
||||
<span class="n">code</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
|
||||
<span class="n">prompt</span> <span class="o">=</span> <span class="n">ps1</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">}</span>
|
||||
<span class="k">else</span> <span class="cm">/* some non-syntax error */</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">PyErr_Print</span> <span class="p">();</span>
|
||||
<span class="n">free</span> <span class="p">(</span><span class="n">code</span><span class="p">);</span>
|
||||
<span class="n">code</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
|
||||
<span class="n">prompt</span> <span class="o">=</span> <span class="n">ps1</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
|
||||
<span class="n">free</span> <span class="p">(</span><span class="n">line</span><span class="p">);</span>
|
||||
<span class="p">}</span>
|
||||
<span class="p">}</span>
|
||||
|
||||
<span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">glb</span><span class="p">);</span>
|
||||
<span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">loc</span><span class="p">);</span>
|
||||
<span class="n">Py_Finalize</span><span class="p">();</span>
|
||||
<span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-find-undefined-g-symbols-builtin-new-or-pure-virtual">
|
||||
<h2><a class="toc-backref" href="#id18">How do I find undefined g++ symbols __builtin_new or __pure_virtual?</a><a class="headerlink" href="#how-do-i-find-undefined-g-symbols-builtin-new-or-pure-virtual" title="Permalink to this headline">¶</a></h2>
|
||||
<p>To dynamically load g++ extension modules, you must recompile Python, relink it
|
||||
using g++ (change LINKCC in the Python Modules Makefile), and link your
|
||||
extension module using g++ (e.g., <code class="docutils literal notranslate"><span class="pre">g++</span> <span class="pre">-shared</span> <span class="pre">-o</span> <span class="pre">mymodule.so</span> <span class="pre">mymodule.o</span></code>).</p>
|
||||
</div>
|
||||
<div class="section" id="can-i-create-an-object-class-with-some-methods-implemented-in-c-and-others-in-python-e-g-through-inheritance">
|
||||
<h2><a class="toc-backref" href="#id19">Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)?</a><a class="headerlink" href="#can-i-create-an-object-class-with-some-methods-implemented-in-c-and-others-in-python-e-g-through-inheritance" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Yes, you can inherit from built-in classes such as <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#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>, etc.</p>
|
||||
<p>The Boost Python Library (BPL, <a class="reference external" href="http://www.boost.org/libs/python/doc/index.html">http://www.boost.org/libs/python/doc/index.html</a>)
|
||||
provides a way of doing this from C++ (i.e. you can inherit from an extension
|
||||
class written in C++ using the BPL).</p>
|
||||
</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="#">Extending/Embedding FAQ</a><ul>
|
||||
<li><a class="reference internal" href="#can-i-create-my-own-functions-in-c">Can I create my own functions in C?</a></li>
|
||||
<li><a class="reference internal" href="#id1">Can I create my own functions in C++?</a></li>
|
||||
<li><a class="reference internal" href="#writing-c-is-hard-are-there-any-alternatives">Writing C is hard; are there any alternatives?</a></li>
|
||||
<li><a class="reference internal" href="#how-can-i-execute-arbitrary-python-statements-from-c">How can I execute arbitrary Python statements from C?</a></li>
|
||||
<li><a class="reference internal" href="#how-can-i-evaluate-an-arbitrary-python-expression-from-c">How can I evaluate an arbitrary Python expression from C?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-extract-c-values-from-a-python-object">How do I extract C values from a Python object?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-use-py-buildvalue-to-create-a-tuple-of-arbitrary-length">How do I use Py_BuildValue() to create a tuple of arbitrary length?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-call-an-object-s-method-from-c">How do I call an object’s method from C?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-catch-the-output-from-pyerr-print-or-anything-that-prints-to-stdout-stderr">How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-access-a-module-written-in-python-from-c">How do I access a module written in Python from C?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-interface-to-c-objects-from-python">How do I interface to C++ objects from Python?</a></li>
|
||||
<li><a class="reference internal" href="#i-added-a-module-using-the-setup-file-and-the-make-fails-why">I added a module using the Setup file and the make fails; why?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-debug-an-extension">How do I debug an extension?</a></li>
|
||||
<li><a class="reference internal" href="#i-want-to-compile-a-python-module-on-my-linux-system-but-some-files-are-missing-why">I want to compile a Python module on my Linux system, but some files are missing. Why?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-tell-incomplete-input-from-invalid-input">How do I tell “incomplete input” from “invalid input”?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-find-undefined-g-symbols-builtin-new-or-pure-virtual">How do I find undefined g++ symbols __builtin_new or __pure_virtual?</a></li>
|
||||
<li><a class="reference internal" href="#can-i-create-an-object-class-with-some-methods-implemented-in-c-and-others-in-python-e-g-through-inheritance">Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)?</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>Previous topic</h4>
|
||||
<p class="topless"><a href="library.html"
|
||||
title="previous chapter">Library and Extension FAQ</a></p>
|
||||
<h4>Next topic</h4>
|
||||
<p class="topless"><a href="windows.html"
|
||||
title="next chapter">Python on Windows 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/extending.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="windows.html" title="Python on Windows FAQ"
|
||||
>next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="library.html" title="Library and Extension 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>
|
572
python-3.7.4-docs-html/faq/general.html
Normal file
572
python-3.7.4-docs-html/faq/general.html
Normal file
@@ -0,0 +1,572 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>General Python 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="Programming FAQ" href="programming.html" />
|
||||
<link rel="prev" title="Python Frequently Asked Questions" href="index.html" />
|
||||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/faq/general.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="programming.html" title="Programming FAQ"
|
||||
accesskey="N">next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="index.html" title="Python Frequently Asked Questions"
|
||||
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="general-python-faq">
|
||||
<h1><a class="toc-backref" href="#id2">General Python FAQ</a><a class="headerlink" href="#general-python-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="#general-python-faq" id="id2">General Python FAQ</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#general-information" id="id3">General Information</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#what-is-python" id="id4">What is Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#what-is-the-python-software-foundation" id="id5">What is the Python Software Foundation?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#are-there-copyright-restrictions-on-the-use-of-python" id="id6">Are there copyright restrictions on the use of Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-was-python-created-in-the-first-place" id="id7">Why was Python created in the first place?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#what-is-python-good-for" id="id8">What is Python good for?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-does-the-python-version-numbering-scheme-work" id="id9">How does the Python version numbering scheme work?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-obtain-a-copy-of-the-python-source" id="id10">How do I obtain a copy of the Python source?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-get-documentation-on-python" id="id11">How do I get documentation on Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#i-ve-never-programmed-before-is-there-a-python-tutorial" id="id12">I’ve never programmed before. Is there a Python tutorial?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#is-there-a-newsgroup-or-mailing-list-devoted-to-python" id="id13">Is there a newsgroup or mailing list devoted to Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-get-a-beta-test-version-of-python" id="id14">How do I get a beta test version of Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-submit-bug-reports-and-patches-for-python" id="id15">How do I submit bug reports and patches for Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#are-there-any-published-articles-about-python-that-i-can-reference" id="id16">Are there any published articles about Python that I can reference?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#are-there-any-books-on-python" id="id17">Are there any books on Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#where-in-the-world-is-www-python-org-located" id="id18">Where in the world is www.python.org located?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-is-it-called-python" id="id19">Why is it called Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#do-i-have-to-like-monty-python-s-flying-circus" id="id20">Do I have to like “Monty Python’s Flying Circus”?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="#python-in-the-real-world" id="id21">Python in the real world</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#how-stable-is-python" id="id22">How stable is Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-many-people-are-using-python" id="id23">How many people are using Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#have-any-significant-projects-been-done-in-python" id="id24">Have any significant projects been done in Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#what-new-developments-are-expected-for-python-in-the-future" id="id25">What new developments are expected for Python in the future?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#is-it-reasonable-to-propose-incompatible-changes-to-python" id="id26">Is it reasonable to propose incompatible changes to Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#is-python-a-good-language-for-beginning-programmers" id="id27">Is Python a good language for beginning programmers?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section" id="general-information">
|
||||
<h2><a class="toc-backref" href="#id3">General Information</a><a class="headerlink" href="#general-information" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="what-is-python">
|
||||
<h3><a class="toc-backref" href="#id4">What is Python?</a><a class="headerlink" href="#what-is-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Python is an interpreted, interactive, object-oriented programming language. It
|
||||
incorporates modules, exceptions, dynamic typing, very high level dynamic data
|
||||
types, and classes. Python combines remarkable power with very clear syntax.
|
||||
It has interfaces to many system calls and libraries, as well as to various
|
||||
window systems, and is extensible in C or C++. It is also usable as an
|
||||
extension language for applications that need a programmable interface.
|
||||
Finally, Python is portable: it runs on many Unix variants, on the Mac, and on
|
||||
Windows 2000 and later.</p>
|
||||
<p>To find out more, start with <a class="reference internal" href="../tutorial/index.html#tutorial-index"><span class="std std-ref">The Python Tutorial</span></a>. The <a class="reference external" href="https://wiki.python.org/moin/BeginnersGuide">Beginner’s Guide to
|
||||
Python</a> links to other
|
||||
introductory tutorials and resources for learning Python.</p>
|
||||
</div>
|
||||
<div class="section" id="what-is-the-python-software-foundation">
|
||||
<h3><a class="toc-backref" href="#id5">What is the Python Software Foundation?</a><a class="headerlink" href="#what-is-the-python-software-foundation" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The Python Software Foundation is an independent non-profit organization that
|
||||
holds the copyright on Python versions 2.1 and newer. The PSF’s mission is to
|
||||
advance open source technology related to the Python programming language and to
|
||||
publicize the use of Python. The PSF’s home page is at
|
||||
<a class="reference external" href="https://www.python.org/psf/">https://www.python.org/psf/</a>.</p>
|
||||
<p>Donations to the PSF are tax-exempt in the US. If you use Python and find it
|
||||
helpful, please contribute via <a class="reference external" href="https://www.python.org/psf/donations/">the PSF donation page</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="are-there-copyright-restrictions-on-the-use-of-python">
|
||||
<h3><a class="toc-backref" href="#id6">Are there copyright restrictions on the use of Python?</a><a class="headerlink" href="#are-there-copyright-restrictions-on-the-use-of-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>You can do anything you want with the source, as long as you leave the
|
||||
copyrights in and display those copyrights in any documentation about Python
|
||||
that you produce. If you honor the copyright rules, it’s OK to use Python for
|
||||
commercial use, to sell copies of Python in source or binary form (modified or
|
||||
unmodified), or to sell products that incorporate Python in some form. We would
|
||||
still like to know about all commercial use of Python, of course.</p>
|
||||
<p>See <a class="reference external" href="https://www.python.org/psf/license/">the PSF license page</a> to find further
|
||||
explanations and a link to the full text of the license.</p>
|
||||
<p>The Python logo is trademarked, and in certain cases permission is required to
|
||||
use it. Consult <a class="reference external" href="https://www.python.org/psf/trademarks/">the Trademark Usage Policy</a> for more information.</p>
|
||||
</div>
|
||||
<div class="section" id="why-was-python-created-in-the-first-place">
|
||||
<h3><a class="toc-backref" href="#id7">Why was Python created in the first place?</a><a class="headerlink" href="#why-was-python-created-in-the-first-place" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Here’s a <em>very</em> brief summary of what started it all, written by Guido van
|
||||
Rossum:</p>
|
||||
<blockquote>
|
||||
<div><p>I had extensive experience with implementing an interpreted language in the
|
||||
ABC group at CWI, and from working with this group I had learned a lot about
|
||||
language design. This is the origin of many Python features, including the
|
||||
use of indentation for statement grouping and the inclusion of
|
||||
very-high-level data types (although the details are all different in
|
||||
Python).</p>
|
||||
<p>I had a number of gripes about the ABC language, but also liked many of its
|
||||
features. It was impossible to extend the ABC language (or its
|
||||
implementation) to remedy my complaints – in fact its lack of extensibility
|
||||
was one of its biggest problems. I had some experience with using Modula-2+
|
||||
and talked with the designers of Modula-3 and read the Modula-3 report.
|
||||
Modula-3 is the origin of the syntax and semantics used for exceptions, and
|
||||
some other Python features.</p>
|
||||
<p>I was working in the Amoeba distributed operating system group at CWI. We
|
||||
needed a better way to do system administration than by writing either C
|
||||
programs or Bourne shell scripts, since Amoeba had its own system call
|
||||
interface which wasn’t easily accessible from the Bourne shell. My
|
||||
experience with error handling in Amoeba made me acutely aware of the
|
||||
importance of exceptions as a programming language feature.</p>
|
||||
<p>It occurred to me that a scripting language with a syntax like ABC but with
|
||||
access to the Amoeba system calls would fill the need. I realized that it
|
||||
would be foolish to write an Amoeba-specific language, so I decided that I
|
||||
needed a language that was generally extensible.</p>
|
||||
<p>During the 1989 Christmas holidays, I had a lot of time on my hand, so I
|
||||
decided to give it a try. During the next year, while still mostly working
|
||||
on it in my own time, Python was used in the Amoeba project with increasing
|
||||
success, and the feedback from colleagues made me add many early
|
||||
improvements.</p>
|
||||
<p>In February 1991, after just over a year of development, I decided to post to
|
||||
USENET. The rest is in the <code class="docutils literal notranslate"><span class="pre">Misc/HISTORY</span></code> file.</p>
|
||||
</div></blockquote>
|
||||
</div>
|
||||
<div class="section" id="what-is-python-good-for">
|
||||
<h3><a class="toc-backref" href="#id8">What is Python good for?</a><a class="headerlink" href="#what-is-python-good-for" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Python is a high-level general-purpose programming language that can be applied
|
||||
to many different classes of problems.</p>
|
||||
<p>The language comes with a large standard library that covers areas such as
|
||||
string processing (regular expressions, Unicode, calculating differences between
|
||||
files), Internet protocols (HTTP, FTP, SMTP, XML-RPC, POP, IMAP, CGI
|
||||
programming), software engineering (unit testing, logging, profiling, parsing
|
||||
Python code), and operating system interfaces (system calls, filesystems, TCP/IP
|
||||
sockets). Look at the table of contents for <a class="reference internal" href="../library/index.html#library-index"><span class="std std-ref">The Python Standard Library</span></a> to get an idea
|
||||
of what’s available. A wide variety of third-party extensions are also
|
||||
available. Consult <a class="reference external" href="https://pypi.org">the Python Package Index</a> to
|
||||
find packages of interest to you.</p>
|
||||
</div>
|
||||
<div class="section" id="how-does-the-python-version-numbering-scheme-work">
|
||||
<h3><a class="toc-backref" href="#id9">How does the Python version numbering scheme work?</a><a class="headerlink" href="#how-does-the-python-version-numbering-scheme-work" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Python versions are numbered A.B.C or A.B. A is the major version number – it
|
||||
is only incremented for really major changes in the language. B is the minor
|
||||
version number, incremented for less earth-shattering changes. C is the
|
||||
micro-level – it is incremented for each bugfix release. See <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0006"><strong>PEP 6</strong></a> for more
|
||||
information about bugfix releases.</p>
|
||||
<p>Not all releases are bugfix releases. In the run-up to a new major release, a
|
||||
series of development releases are made, denoted as alpha, beta, or release
|
||||
candidate. Alphas are early releases in which interfaces aren’t yet finalized;
|
||||
it’s not unexpected to see an interface change between two alpha releases.
|
||||
Betas are more stable, preserving existing interfaces but possibly adding new
|
||||
modules, and release candidates are frozen, making no changes except as needed
|
||||
to fix critical bugs.</p>
|
||||
<p>Alpha, beta and release candidate versions have an additional suffix. The
|
||||
suffix for an alpha version is “aN” for some small number N, the suffix for a
|
||||
beta version is “bN” for some small number N, and the suffix for a release
|
||||
candidate version is “cN” for some small number N. In other words, all versions
|
||||
labeled 2.0aN precede the versions labeled 2.0bN, which precede versions labeled
|
||||
2.0cN, and <em>those</em> precede 2.0.</p>
|
||||
<p>You may also find version numbers with a “+” suffix, e.g. “2.2+”. These are
|
||||
unreleased versions, built directly from the CPython development repository. In
|
||||
practice, after a final minor release is made, the version is incremented to the
|
||||
next minor version, which becomes the “a0” version, e.g. “2.4a0”.</p>
|
||||
<p>See also the documentation for <a class="reference internal" href="../library/sys.html#sys.version" title="sys.version"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.version</span></code></a>, <a class="reference internal" href="../library/sys.html#sys.hexversion" title="sys.hexversion"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.hexversion</span></code></a>, and
|
||||
<a class="reference internal" href="../library/sys.html#sys.version_info" title="sys.version_info"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.version_info</span></code></a>.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-obtain-a-copy-of-the-python-source">
|
||||
<h3><a class="toc-backref" href="#id10">How do I obtain a copy of the Python source?</a><a class="headerlink" href="#how-do-i-obtain-a-copy-of-the-python-source" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The latest Python source distribution is always available from python.org, at
|
||||
<a class="reference external" href="https://www.python.org/downloads/">https://www.python.org/downloads/</a>. The latest development sources can be obtained
|
||||
at <a class="reference external" href="https://github.com/python/cpython/">https://github.com/python/cpython/</a>.</p>
|
||||
<p>The source distribution is a gzipped tar file containing the complete C source,
|
||||
Sphinx-formatted documentation, Python library modules, example programs, and
|
||||
several useful pieces of freely distributable software. The source will compile
|
||||
and run out of the box on most UNIX platforms.</p>
|
||||
<p>Consult the <a class="reference external" href="https://devguide.python.org/setup/">Getting Started section of the Python Developer’s Guide</a> for more
|
||||
information on getting the source code and compiling it.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-get-documentation-on-python">
|
||||
<h3><a class="toc-backref" href="#id11">How do I get documentation on Python?</a><a class="headerlink" href="#how-do-i-get-documentation-on-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The standard documentation for the current stable version of Python is available
|
||||
at <a class="reference external" href="https://docs.python.org/3/">https://docs.python.org/3/</a>. PDF, plain text, and downloadable HTML versions are
|
||||
also available at <a class="reference external" href="https://docs.python.org/3/download.html">https://docs.python.org/3/download.html</a>.</p>
|
||||
<p>The documentation is written in reStructuredText and processed by <a class="reference external" href="http://sphinx-doc.org/">the Sphinx
|
||||
documentation tool</a>. The reStructuredText source for
|
||||
the documentation is part of the Python source distribution.</p>
|
||||
</div>
|
||||
<div class="section" id="i-ve-never-programmed-before-is-there-a-python-tutorial">
|
||||
<h3><a class="toc-backref" href="#id12">I’ve never programmed before. Is there a Python tutorial?</a><a class="headerlink" href="#i-ve-never-programmed-before-is-there-a-python-tutorial" title="Permalink to this headline">¶</a></h3>
|
||||
<p>There are numerous tutorials and books available. The standard documentation
|
||||
includes <a class="reference internal" href="../tutorial/index.html#tutorial-index"><span class="std std-ref">The Python Tutorial</span></a>.</p>
|
||||
<p>Consult <a class="reference external" href="https://wiki.python.org/moin/BeginnersGuide">the Beginner’s Guide</a> to
|
||||
find information for beginning Python programmers, including lists of tutorials.</p>
|
||||
</div>
|
||||
<div class="section" id="is-there-a-newsgroup-or-mailing-list-devoted-to-python">
|
||||
<h3><a class="toc-backref" href="#id13">Is there a newsgroup or mailing list devoted to Python?</a><a class="headerlink" href="#is-there-a-newsgroup-or-mailing-list-devoted-to-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>There is a newsgroup, <em class="newsgroup">comp.lang.python</em>, and a mailing list,
|
||||
<a class="reference external" href="https://mail.python.org/mailman/listinfo/python-list">python-list</a>. The
|
||||
newsgroup and mailing list are gatewayed into each other – if you can read news
|
||||
it’s unnecessary to subscribe to the mailing list.
|
||||
<em class="newsgroup">comp.lang.python</em> is high-traffic, receiving hundreds of postings
|
||||
every day, and Usenet readers are often more able to cope with this volume.</p>
|
||||
<p>Announcements of new software releases and events can be found in
|
||||
comp.lang.python.announce, a low-traffic moderated list that receives about five
|
||||
postings per day. It’s available as <a class="reference external" href="https://mail.python.org/mailman/listinfo/python-announce-list">the python-announce mailing list</a>.</p>
|
||||
<p>More info about other mailing lists and newsgroups
|
||||
can be found at <a class="reference external" href="https://www.python.org/community/lists/">https://www.python.org/community/lists/</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-get-a-beta-test-version-of-python">
|
||||
<h3><a class="toc-backref" href="#id14">How do I get a beta test version of Python?</a><a class="headerlink" href="#how-do-i-get-a-beta-test-version-of-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Alpha and beta releases are available from <a class="reference external" href="https://www.python.org/downloads/">https://www.python.org/downloads/</a>. All
|
||||
releases are announced on the comp.lang.python and comp.lang.python.announce
|
||||
newsgroups and on the Python home page at <a class="reference external" href="https://www.python.org/">https://www.python.org/</a>; an RSS feed of
|
||||
news is available.</p>
|
||||
<p>You can also access the development version of Python through Git. See
|
||||
<a class="reference external" href="https://devguide.python.org/">The Python Developer’s Guide</a> for details.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-submit-bug-reports-and-patches-for-python">
|
||||
<h3><a class="toc-backref" href="#id15">How do I submit bug reports and patches for Python?</a><a class="headerlink" href="#how-do-i-submit-bug-reports-and-patches-for-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>To report a bug or submit a patch, please use the Roundup installation at
|
||||
<a class="reference external" href="https://bugs.python.org/">https://bugs.python.org/</a>.</p>
|
||||
<p>You must have a Roundup account to report bugs; this makes it possible for us to
|
||||
contact you if we have follow-up questions. It will also enable Roundup to send
|
||||
you updates as we act on your bug. If you had previously used SourceForge to
|
||||
report bugs to Python, you can obtain your Roundup password through Roundup’s
|
||||
<a class="reference external" href="https://bugs.python.org/user?@template=forgotten">password reset procedure</a>.</p>
|
||||
<p>For more information on how Python is developed, consult <a class="reference external" href="https://devguide.python.org/">the Python Developer’s
|
||||
Guide</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="are-there-any-published-articles-about-python-that-i-can-reference">
|
||||
<h3><a class="toc-backref" href="#id16">Are there any published articles about Python that I can reference?</a><a class="headerlink" href="#are-there-any-published-articles-about-python-that-i-can-reference" title="Permalink to this headline">¶</a></h3>
|
||||
<p>It’s probably best to cite your favorite book about Python.</p>
|
||||
<p>The very first article about Python was written in 1991 and is now quite
|
||||
outdated.</p>
|
||||
<blockquote>
|
||||
<div><p>Guido van Rossum and Jelke de Boer, “Interactively Testing Remote Servers
|
||||
Using the Python Programming Language”, CWI Quarterly, Volume 4, Issue 4
|
||||
(December 1991), Amsterdam, pp 283–303.</p>
|
||||
</div></blockquote>
|
||||
</div>
|
||||
<div class="section" id="are-there-any-books-on-python">
|
||||
<h3><a class="toc-backref" href="#id17">Are there any books on Python?</a><a class="headerlink" href="#are-there-any-books-on-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Yes, there are many, and more are being published. See the python.org wiki at
|
||||
<a class="reference external" href="https://wiki.python.org/moin/PythonBooks">https://wiki.python.org/moin/PythonBooks</a> for a list.</p>
|
||||
<p>You can also search online bookstores for “Python” and filter out the Monty
|
||||
Python references; or perhaps search for “Python” and “language”.</p>
|
||||
</div>
|
||||
<div class="section" id="where-in-the-world-is-www-python-org-located">
|
||||
<h3><a class="toc-backref" href="#id18">Where in the world is www.python.org located?</a><a class="headerlink" href="#where-in-the-world-is-www-python-org-located" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The Python project’s infrastructure is located all over the world and is managed
|
||||
by the Python Infrastructure Team. Details <a class="reference external" href="http://infra.psf.io">here</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="why-is-it-called-python">
|
||||
<h3><a class="toc-backref" href="#id19">Why is it called Python?</a><a class="headerlink" href="#why-is-it-called-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>When he began implementing Python, Guido van Rossum was also reading the
|
||||
published scripts from <a class="reference external" href="https://en.wikipedia.org/wiki/Monty_Python">“Monty Python’s Flying Circus”</a>, a BBC comedy series from the 1970s. Van Rossum
|
||||
thought he needed a name that was short, unique, and slightly mysterious, so he
|
||||
decided to call the language Python.</p>
|
||||
</div>
|
||||
<div class="section" id="do-i-have-to-like-monty-python-s-flying-circus">
|
||||
<h3><a class="toc-backref" href="#id20">Do I have to like “Monty Python’s Flying Circus”?</a><a class="headerlink" href="#do-i-have-to-like-monty-python-s-flying-circus" title="Permalink to this headline">¶</a></h3>
|
||||
<p>No, but it helps. :)</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="python-in-the-real-world">
|
||||
<h2><a class="toc-backref" href="#id21">Python in the real world</a><a class="headerlink" href="#python-in-the-real-world" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="how-stable-is-python">
|
||||
<h3><a class="toc-backref" href="#id22">How stable is Python?</a><a class="headerlink" href="#how-stable-is-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Very stable. New, stable releases have been coming out roughly every 6 to 18
|
||||
months since 1991, and this seems likely to continue. Currently there are
|
||||
usually around 18 months between major releases.</p>
|
||||
<p>The developers issue “bugfix” releases of older versions, so the stability of
|
||||
existing releases gradually improves. Bugfix releases, indicated by a third
|
||||
component of the version number (e.g. 3.5.3, 3.6.2), are managed for stability;
|
||||
only fixes for known problems are included in a bugfix release, and it’s
|
||||
guaranteed that interfaces will remain the same throughout a series of bugfix
|
||||
releases.</p>
|
||||
<p>The latest stable releases can always be found on the <a class="reference external" href="https://www.python.org/downloads/">Python download page</a>. There are two production-ready versions
|
||||
of Python: 2.x and 3.x. The recommended version is 3.x, which is supported by
|
||||
most widely used libraries. Although 2.x is still widely used, <a class="reference external" href="https://www.python.org/dev/peps/pep-0373/">it will not
|
||||
be maintained after January 1, 2020</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="how-many-people-are-using-python">
|
||||
<h3><a class="toc-backref" href="#id23">How many people are using Python?</a><a class="headerlink" href="#how-many-people-are-using-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>There are probably tens of thousands of users, though it’s difficult to obtain
|
||||
an exact count.</p>
|
||||
<p>Python is available for free download, so there are no sales figures, and it’s
|
||||
available from many different sites and packaged with many Linux distributions,
|
||||
so download statistics don’t tell the whole story either.</p>
|
||||
<p>The comp.lang.python newsgroup is very active, but not all Python users post to
|
||||
the group or even read it.</p>
|
||||
</div>
|
||||
<div class="section" id="have-any-significant-projects-been-done-in-python">
|
||||
<h3><a class="toc-backref" href="#id24">Have any significant projects been done in Python?</a><a class="headerlink" href="#have-any-significant-projects-been-done-in-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>See <a class="reference external" href="https://www.python.org/about/success">https://www.python.org/about/success</a> for a list of projects that use Python.
|
||||
Consulting the proceedings for <a class="reference external" href="https://www.python.org/community/workshops/">past Python conferences</a> will reveal contributions from many
|
||||
different companies and organizations.</p>
|
||||
<p>High-profile Python projects include <a class="reference external" href="http://www.list.org">the Mailman mailing list manager</a> and <a class="reference external" href="http://www.zope.org">the Zope application server</a>. Several Linux distributions, most notably <a class="reference external" href="https://www.redhat.com">Red Hat</a>, have written part or all of their installer and
|
||||
system administration software in Python. Companies that use Python internally
|
||||
include Google, Yahoo, and Lucasfilm Ltd.</p>
|
||||
</div>
|
||||
<div class="section" id="what-new-developments-are-expected-for-python-in-the-future">
|
||||
<h3><a class="toc-backref" href="#id25">What new developments are expected for Python in the future?</a><a class="headerlink" href="#what-new-developments-are-expected-for-python-in-the-future" title="Permalink to this headline">¶</a></h3>
|
||||
<p>See <a class="reference external" href="https://www.python.org/dev/peps/">https://www.python.org/dev/peps/</a> for the Python Enhancement Proposals
|
||||
(PEPs). PEPs are design documents describing a suggested new feature for Python,
|
||||
providing a concise technical specification and a rationale. Look for a PEP
|
||||
titled “Python X.Y Release Schedule”, where X.Y is a version that hasn’t been
|
||||
publicly released yet.</p>
|
||||
<p>New development is discussed on <a class="reference external" href="https://mail.python.org/mailman/listinfo/python-dev/">the python-dev mailing list</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="is-it-reasonable-to-propose-incompatible-changes-to-python">
|
||||
<h3><a class="toc-backref" href="#id26">Is it reasonable to propose incompatible changes to Python?</a><a class="headerlink" href="#is-it-reasonable-to-propose-incompatible-changes-to-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>In general, no. There are already millions of lines of Python code around the
|
||||
world, so any change in the language that invalidates more than a very small
|
||||
fraction of existing programs has to be frowned upon. Even if you can provide a
|
||||
conversion program, there’s still the problem of updating all documentation;
|
||||
many books have been written about Python, and we don’t want to invalidate them
|
||||
all at a single stroke.</p>
|
||||
<p>Providing a gradual upgrade path is necessary if a feature has to be changed.
|
||||
<span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0005"><strong>PEP 5</strong></a> describes the procedure followed for introducing backward-incompatible
|
||||
changes while minimizing disruption for users.</p>
|
||||
</div>
|
||||
<div class="section" id="is-python-a-good-language-for-beginning-programmers">
|
||||
<h3><a class="toc-backref" href="#id27">Is Python a good language for beginning programmers?</a><a class="headerlink" href="#is-python-a-good-language-for-beginning-programmers" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Yes.</p>
|
||||
<p>It is still common to start students with a procedural and statically typed
|
||||
language such as Pascal, C, or a subset of C++ or Java. Students may be better
|
||||
served by learning Python as their first language. Python has a very simple and
|
||||
consistent syntax and a large standard library and, most importantly, using
|
||||
Python in a beginning programming course lets students concentrate on important
|
||||
programming skills such as problem decomposition and data type design. With
|
||||
Python, students can be quickly introduced to basic concepts such as loops and
|
||||
procedures. They can probably even work with user-defined objects in their very
|
||||
first course.</p>
|
||||
<p>For a student who has never programmed before, using a statically typed language
|
||||
seems unnatural. It presents additional complexity that the student must master
|
||||
and slows the pace of the course. The students are trying to learn to think
|
||||
like a computer, decompose problems, design consistent interfaces, and
|
||||
encapsulate data. While learning to use a statically typed language is
|
||||
important in the long term, it is not necessarily the best topic to address in
|
||||
the students’ first programming course.</p>
|
||||
<p>Many other aspects of Python make it a good first language. Like Java, Python
|
||||
has a large standard library so that students can be assigned programming
|
||||
projects very early in the course that <em>do</em> something. Assignments aren’t
|
||||
restricted to the standard four-function calculator and check balancing
|
||||
programs. By using the standard library, students can gain the satisfaction of
|
||||
working on realistic applications as they learn the fundamentals of programming.
|
||||
Using the standard library also teaches students about code reuse. Third-party
|
||||
modules such as PyGame are also helpful in extending the students’ reach.</p>
|
||||
<p>Python’s interactive interpreter enables students to test language features
|
||||
while they’re programming. They can keep a window with the interpreter running
|
||||
while they enter their program’s source in another window. If they can’t
|
||||
remember the methods for a list, they can do something like this:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">L</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="gp">>>> </span><span class="nb">dir</span><span class="p">(</span><span class="n">L</span><span class="p">)</span>
|
||||
<span class="go">['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',</span>
|
||||
<span class="go">'__dir__', '__doc__', '__eq__', '__format__', '__ge__',</span>
|
||||
<span class="go">'__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__',</span>
|
||||
<span class="go">'__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__',</span>
|
||||
<span class="go">'__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',</span>
|
||||
<span class="go">'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__',</span>
|
||||
<span class="go">'__sizeof__', '__str__', '__subclasshook__', 'append', 'clear',</span>
|
||||
<span class="go">'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove',</span>
|
||||
<span class="go">'reverse', 'sort']</span>
|
||||
<span class="gp">>>> </span><span class="p">[</span><span class="n">d</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="n">L</span><span class="p">)</span> <span class="k">if</span> <span class="s1">'__'</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">d</span><span class="p">]</span>
|
||||
<span class="go">['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']</span>
|
||||
|
||||
<span class="gp">>>> </span><span class="n">help</span><span class="p">(</span><span class="n">L</span><span class="o">.</span><span class="n">append</span><span class="p">)</span>
|
||||
<span class="go">Help on built-in function append:</span>
|
||||
|
||||
<span class="go">append(...)</span>
|
||||
<span class="go"> L.append(object) -> None -- append object to end</span>
|
||||
|
||||
<span class="gp">>>> </span><span class="n">L</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
||||
<span class="gp">>>> </span><span class="n">L</span>
|
||||
<span class="go">[1]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>With the interpreter, documentation is never far from the student as they are
|
||||
programming.</p>
|
||||
<p>There are also good IDEs for Python. IDLE is a cross-platform IDE for Python
|
||||
that is written in Python using Tkinter. PythonWin is a Windows-specific IDE.
|
||||
Emacs users will be happy to know that there is a very good Python mode for
|
||||
Emacs. All of these programming environments provide syntax highlighting,
|
||||
auto-indenting, and access to the interactive interpreter while coding. Consult
|
||||
<a class="reference external" href="https://wiki.python.org/moin/PythonEditors">the Python wiki</a> for a full list
|
||||
of Python editing environments.</p>
|
||||
<p>If you want to discuss Python’s use in education, you may be interested in
|
||||
joining <a class="reference external" href="https://www.python.org/community/sigs/current/edu-sig">the edu-sig mailing list</a>.</p>
|
||||
</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="#">General Python FAQ</a><ul>
|
||||
<li><a class="reference internal" href="#general-information">General Information</a></li>
|
||||
<li><a class="reference internal" href="#python-in-the-real-world">Python in the real world</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>Previous topic</h4>
|
||||
<p class="topless"><a href="index.html"
|
||||
title="previous chapter">Python Frequently Asked Questions</a></p>
|
||||
<h4>Next topic</h4>
|
||||
<p class="topless"><a href="programming.html"
|
||||
title="next chapter">Programming 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/general.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="programming.html" title="Programming FAQ"
|
||||
>next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="index.html" title="Python Frequently Asked Questions"
|
||||
>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>
|
336
python-3.7.4-docs-html/faq/gui.html
Normal file
336
python-3.7.4-docs-html/faq/gui.html
Normal file
@@ -0,0 +1,336 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Graphic User Interface 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="“Why is Python Installed on my Computer?” FAQ" href="installed.html" />
|
||||
<link rel="prev" title="Python on Windows FAQ" href="windows.html" />
|
||||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/faq/gui.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="installed.html" title="“Why is Python Installed on my Computer?” FAQ"
|
||||
accesskey="N">next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="windows.html" title="Python on Windows 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="graphic-user-interface-faq">
|
||||
<h1><a class="toc-backref" href="#id2">Graphic User Interface FAQ</a><a class="headerlink" href="#graphic-user-interface-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="#graphic-user-interface-faq" id="id2">Graphic User Interface FAQ</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#general-gui-questions" id="id3">General GUI Questions</a></p></li>
|
||||
<li><p><a class="reference internal" href="#what-platform-independent-gui-toolkits-exist-for-python" id="id4">What platform-independent GUI toolkits exist for Python?</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#tkinter" id="id5">Tkinter</a></p></li>
|
||||
<li><p><a class="reference internal" href="#wxwidgets" id="id6">wxWidgets</a></p></li>
|
||||
<li><p><a class="reference internal" href="#qt" id="id7">Qt</a></p></li>
|
||||
<li><p><a class="reference internal" href="#gtk" id="id8">Gtk+</a></p></li>
|
||||
<li><p><a class="reference internal" href="#kivy" id="id9">Kivy</a></p></li>
|
||||
<li><p><a class="reference internal" href="#fltk" id="id10">FLTK</a></p></li>
|
||||
<li><p><a class="reference internal" href="#opengl" id="id11">OpenGL</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="#what-platform-specific-gui-toolkits-exist-for-python" id="id12">What platform-specific GUI toolkits exist for Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#tkinter-questions" id="id13">Tkinter questions</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#how-do-i-freeze-tkinter-applications" id="id14">How do I freeze Tkinter applications?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#can-i-have-tk-events-handled-while-waiting-for-i-o" id="id15">Can I have Tk events handled while waiting for I/O?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#i-can-t-get-key-bindings-to-work-in-tkinter-why" id="id16">I can’t get key bindings to work in Tkinter: why?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section" id="general-gui-questions">
|
||||
<h2><a class="toc-backref" href="#id3">General GUI Questions</a><a class="headerlink" href="#general-gui-questions" title="Permalink to this headline">¶</a></h2>
|
||||
</div>
|
||||
<div class="section" id="what-platform-independent-gui-toolkits-exist-for-python">
|
||||
<h2><a class="toc-backref" href="#id4">What platform-independent GUI toolkits exist for Python?</a><a class="headerlink" href="#what-platform-independent-gui-toolkits-exist-for-python" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Depending on what platform(s) you are aiming at, there are several. Some
|
||||
of them haven’t been ported to Python 3 yet. At least <a class="reference internal" href="#tkinter">Tkinter</a> and <a class="reference internal" href="#qt">Qt</a>
|
||||
are known to be Python 3-compatible.</p>
|
||||
<div class="section" id="tkinter">
|
||||
<h3><a class="toc-backref" href="#id5">Tkinter</a><a class="headerlink" href="#tkinter" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Standard builds of Python include an object-oriented interface to the Tcl/Tk
|
||||
widget set, called <a class="reference internal" href="../library/tk.html#tkinter"><span class="std std-ref">tkinter</span></a>. This is probably the easiest to
|
||||
install (since it comes included with most
|
||||
<a class="reference external" href="https://www.python.org/downloads/">binary distributions</a> of Python) and use.
|
||||
For more info about Tk, including pointers to the source, see the
|
||||
<a class="reference external" href="https://www.tcl.tk">Tcl/Tk home page</a>. Tcl/Tk is fully portable to the
|
||||
Mac OS X, Windows, and Unix platforms.</p>
|
||||
</div>
|
||||
<div class="section" id="wxwidgets">
|
||||
<h3><a class="toc-backref" href="#id6">wxWidgets</a><a class="headerlink" href="#wxwidgets" title="Permalink to this headline">¶</a></h3>
|
||||
<p>wxWidgets (<a class="reference external" href="https://www.wxwidgets.org">https://www.wxwidgets.org</a>) is a free, portable GUI class
|
||||
library written in C++ that provides a native look and feel on a
|
||||
number of platforms, with Windows, Mac OS X, GTK, X11, all listed as
|
||||
current stable targets. Language bindings are available for a number
|
||||
of languages including Python, Perl, Ruby, etc.</p>
|
||||
<p><a class="reference external" href="https://www.wxpython.org">wxPython</a> is the Python binding for
|
||||
wxwidgets. While it often lags slightly behind the official wxWidgets
|
||||
releases, it also offers a number of features via pure Python
|
||||
extensions that are not available in other language bindings. There
|
||||
is an active wxPython user and developer community.</p>
|
||||
<p>Both wxWidgets and wxPython are free, open source, software with
|
||||
permissive licences that allow their use in commercial products as
|
||||
well as in freeware or shareware.</p>
|
||||
</div>
|
||||
<div class="section" id="qt">
|
||||
<h3><a class="toc-backref" href="#id7">Qt</a><a class="headerlink" href="#qt" title="Permalink to this headline">¶</a></h3>
|
||||
<p>There are bindings available for the Qt toolkit (using either <a class="reference external" href="https://riverbankcomputing.com/software/pyqt/intro">PyQt</a> or <a class="reference external" href="https://wiki.qt.io/PySide">PySide</a>) and for KDE (<a class="reference external" href="https://techbase.kde.org/Languages/Python/Using_PyKDE_4">PyKDE4</a>).
|
||||
PyQt is currently more mature than PySide, but you must buy a PyQt license from
|
||||
<a class="reference external" href="https://www.riverbankcomputing.com/commercial/license-faq">Riverbank Computing</a>
|
||||
if you want to write proprietary applications. PySide is free for all applications.</p>
|
||||
<p>Qt 4.5 upwards is licensed under the LGPL license; also, commercial licenses
|
||||
are available from <a class="reference external" href="https://www.qt.io/licensing/">The Qt Company</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="gtk">
|
||||
<h3><a class="toc-backref" href="#id8">Gtk+</a><a class="headerlink" href="#gtk" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The <a class="reference external" href="https://wiki.gnome.org/Projects/PyGObject">GObject introspection bindings</a>
|
||||
for Python allow you to write GTK+ 3 applications. There is also a
|
||||
<a class="reference external" href="https://python-gtk-3-tutorial.readthedocs.io">Python GTK+ 3 Tutorial</a>.</p>
|
||||
<p>The older PyGtk bindings for the <a class="reference external" href="https://www.gtk.org">Gtk+ 2 toolkit</a> have
|
||||
been implemented by James Henstridge; see <<a class="reference external" href="http://www.pygtk.org">http://www.pygtk.org</a>>.</p>
|
||||
</div>
|
||||
<div class="section" id="kivy">
|
||||
<h3><a class="toc-backref" href="#id9">Kivy</a><a class="headerlink" href="#kivy" title="Permalink to this headline">¶</a></h3>
|
||||
<p><a class="reference external" href="https://kivy.org/">Kivy</a> is a cross-platform GUI library supporting both
|
||||
desktop operating systems (Windows, macOS, Linux) and mobile devices (Android,
|
||||
iOS). It is written in Python and Cython, and can use a range of windowing
|
||||
backends.</p>
|
||||
<p>Kivy is free and open source software distributed under the MIT license.</p>
|
||||
</div>
|
||||
<div class="section" id="fltk">
|
||||
<h3><a class="toc-backref" href="#id10">FLTK</a><a class="headerlink" href="#fltk" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Python bindings for <a class="reference external" href="http://www.fltk.org">the FLTK toolkit</a>, a simple yet
|
||||
powerful and mature cross-platform windowing system, are available from <a class="reference external" href="http://pyfltk.sourceforge.net">the
|
||||
PyFLTK project</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="opengl">
|
||||
<h3><a class="toc-backref" href="#id11">OpenGL</a><a class="headerlink" href="#opengl" title="Permalink to this headline">¶</a></h3>
|
||||
<p>For OpenGL bindings, see <a class="reference external" href="http://pyopengl.sourceforge.net">PyOpenGL</a>.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="what-platform-specific-gui-toolkits-exist-for-python">
|
||||
<h2><a class="toc-backref" href="#id12">What platform-specific GUI toolkits exist for Python?</a><a class="headerlink" href="#what-platform-specific-gui-toolkits-exist-for-python" title="Permalink to this headline">¶</a></h2>
|
||||
<p>By installing the <a class="reference external" href="https://pypi.org/project/pyobjc/">PyObjc Objective-C bridge</a>, Python programs can use Mac OS X’s
|
||||
Cocoa libraries.</p>
|
||||
<p><a class="reference internal" href="windows.html#windows-faq"><span class="std std-ref">Pythonwin</span></a> by Mark Hammond includes an interface to the
|
||||
Microsoft Foundation Classes and a Python programming environment
|
||||
that’s written mostly in Python using the MFC classes.</p>
|
||||
</div>
|
||||
<div class="section" id="tkinter-questions">
|
||||
<h2><a class="toc-backref" href="#id13">Tkinter questions</a><a class="headerlink" href="#tkinter-questions" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="how-do-i-freeze-tkinter-applications">
|
||||
<h3><a class="toc-backref" href="#id14">How do I freeze Tkinter applications?</a><a class="headerlink" href="#how-do-i-freeze-tkinter-applications" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Freeze is a tool to create stand-alone applications. When freezing Tkinter
|
||||
applications, the applications will not be truly stand-alone, as the application
|
||||
will still need the Tcl and Tk libraries.</p>
|
||||
<p>One solution is to ship the application with the Tcl and Tk libraries, and point
|
||||
to them at run-time using the <span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TCL_LIBRARY</span></code> and <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TK_LIBRARY</span></code>
|
||||
environment variables.</p>
|
||||
<p>To get truly stand-alone applications, the Tcl scripts that form the library
|
||||
have to be integrated into the application as well. One tool supporting that is
|
||||
SAM (stand-alone modules), which is part of the Tix distribution
|
||||
(<a class="reference external" href="http://tix.sourceforge.net/">http://tix.sourceforge.net/</a>).</p>
|
||||
<p>Build Tix with SAM enabled, perform the appropriate call to
|
||||
<code class="xref c c-func docutils literal notranslate"><span class="pre">Tclsam_init()</span></code>, etc. inside Python’s
|
||||
<code class="file docutils literal notranslate"><span class="pre">Modules/tkappinit.c</span></code>, and link with libtclsam and libtksam (you
|
||||
might include the Tix libraries as well).</p>
|
||||
</div>
|
||||
<div class="section" id="can-i-have-tk-events-handled-while-waiting-for-i-o">
|
||||
<h3><a class="toc-backref" href="#id15">Can I have Tk events handled while waiting for I/O?</a><a class="headerlink" href="#can-i-have-tk-events-handled-while-waiting-for-i-o" title="Permalink to this headline">¶</a></h3>
|
||||
<p>On platforms other than Windows, yes, and you don’t even
|
||||
need threads! But you’ll have to restructure your I/O
|
||||
code a bit. Tk has the equivalent of Xt’s <code class="xref c c-func docutils literal notranslate"><span class="pre">XtAddInput()</span></code> call, which allows you
|
||||
to register a callback function which will be called from the Tk mainloop when
|
||||
I/O is possible on a file descriptor. See <a class="reference internal" href="../library/tkinter.html#tkinter-file-handlers"><span class="std std-ref">File Handlers</span></a>.</p>
|
||||
</div>
|
||||
<div class="section" id="i-can-t-get-key-bindings-to-work-in-tkinter-why">
|
||||
<h3><a class="toc-backref" href="#id16">I can’t get key bindings to work in Tkinter: why?</a><a class="headerlink" href="#i-can-t-get-key-bindings-to-work-in-tkinter-why" title="Permalink to this headline">¶</a></h3>
|
||||
<p>An often-heard complaint is that event handlers bound to events with the
|
||||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">bind()</span></code> method don’t get handled even when the appropriate key is pressed.</p>
|
||||
<p>The most common cause is that the widget to which the binding applies doesn’t
|
||||
have “keyboard focus”. Check out the Tk documentation for the focus command.
|
||||
Usually a widget is given the keyboard focus by clicking in it (but not for
|
||||
labels; see the takefocus option).</p>
|
||||
</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="#">Graphic User Interface FAQ</a><ul>
|
||||
<li><a class="reference internal" href="#general-gui-questions">General GUI Questions</a></li>
|
||||
<li><a class="reference internal" href="#what-platform-independent-gui-toolkits-exist-for-python">What platform-independent GUI toolkits exist for Python?</a></li>
|
||||
<li><a class="reference internal" href="#what-platform-specific-gui-toolkits-exist-for-python">What platform-specific GUI toolkits exist for Python?</a></li>
|
||||
<li><a class="reference internal" href="#tkinter-questions">Tkinter questions</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>Previous topic</h4>
|
||||
<p class="topless"><a href="windows.html"
|
||||
title="previous chapter">Python on Windows FAQ</a></p>
|
||||
<h4>Next topic</h4>
|
||||
<p class="topless"><a href="installed.html"
|
||||
title="next chapter">“Why is Python Installed on my Computer?” 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/gui.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="installed.html" title="“Why is Python Installed on my Computer?” FAQ"
|
||||
>next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="windows.html" title="Python on Windows 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>
|
191
python-3.7.4-docs-html/faq/index.html
Normal file
191
python-3.7.4-docs-html/faq/index.html
Normal file
@@ -0,0 +1,191 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Python Frequently Asked Questions — 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="General Python FAQ" href="general.html" />
|
||||
<link rel="prev" title="Instrumenting CPython with DTrace and SystemTap" href="../howto/instrumentation.html" />
|
||||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/faq/index.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="general.html" title="General Python FAQ"
|
||||
accesskey="N">next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="../howto/instrumentation.html" title="Instrumenting CPython with DTrace and SystemTap"
|
||||
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="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="python-frequently-asked-questions">
|
||||
<span id="faq-index"></span><h1>Python Frequently Asked Questions<a class="headerlink" href="#python-frequently-asked-questions" title="Permalink to this headline">¶</a></h1>
|
||||
<div class="toctree-wrapper compound">
|
||||
<ul>
|
||||
<li class="toctree-l1"><a class="reference internal" href="general.html">General Python FAQ</a></li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="programming.html">Programming FAQ</a></li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="design.html">Design and History FAQ</a></li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="library.html">Library and Extension FAQ</a></li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="extending.html">Extending/Embedding FAQ</a></li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="windows.html">Python on Windows FAQ</a></li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="gui.html">Graphic User Interface FAQ</a></li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="installed.html">“Why is Python Installed on my Computer?” FAQ</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||||
<div class="sphinxsidebarwrapper">
|
||||
<h4>Previous topic</h4>
|
||||
<p class="topless"><a href="../howto/instrumentation.html"
|
||||
title="previous chapter">Instrumenting CPython with DTrace and SystemTap</a></p>
|
||||
<h4>Next topic</h4>
|
||||
<p class="topless"><a href="general.html"
|
||||
title="next chapter">General Python 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/index.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="general.html" title="General Python FAQ"
|
||||
>next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="../howto/instrumentation.html" title="Instrumenting CPython with DTrace and SystemTap"
|
||||
>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="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>
|
232
python-3.7.4-docs-html/faq/installed.html
Normal file
232
python-3.7.4-docs-html/faq/installed.html
Normal file
@@ -0,0 +1,232 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>“Why is Python Installed on my Computer?” 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="Glossary" href="../glossary.html" />
|
||||
<link rel="prev" title="Graphic User Interface FAQ" href="gui.html" />
|
||||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/faq/installed.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="../glossary.html" title="Glossary"
|
||||
accesskey="N">next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="gui.html" title="Graphic User Interface 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="why-is-python-installed-on-my-computer-faq">
|
||||
<h1>“Why is Python Installed on my Computer?” FAQ<a class="headerlink" href="#why-is-python-installed-on-my-computer-faq" title="Permalink to this headline">¶</a></h1>
|
||||
<div class="section" id="what-is-python">
|
||||
<h2>What is Python?<a class="headerlink" href="#what-is-python" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Python is a programming language. It’s used for many different applications.
|
||||
It’s used in some high schools and colleges as an introductory programming
|
||||
language because Python is easy to learn, but it’s also used by professional
|
||||
software developers at places such as Google, NASA, and Lucasfilm Ltd.</p>
|
||||
<p>If you wish to learn more about Python, start with the <a class="reference external" href="https://wiki.python.org/moin/BeginnersGuide">Beginner’s Guide to
|
||||
Python</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="why-is-python-installed-on-my-machine">
|
||||
<h2>Why is Python installed on my machine?<a class="headerlink" href="#why-is-python-installed-on-my-machine" title="Permalink to this headline">¶</a></h2>
|
||||
<p>If you find Python installed on your system but don’t remember installing it,
|
||||
there are several possible ways it could have gotten there.</p>
|
||||
<ul class="simple">
|
||||
<li><p>Perhaps another user on the computer wanted to learn programming and installed
|
||||
it; you’ll have to figure out who’s been using the machine and might have
|
||||
installed it.</p></li>
|
||||
<li><p>A third-party application installed on the machine might have been written in
|
||||
Python and included a Python installation. There are many such applications,
|
||||
from GUI programs to network servers and administrative scripts.</p></li>
|
||||
<li><p>Some Windows machines also have Python installed. At this writing we’re aware
|
||||
of computers from Hewlett-Packard and Compaq that include Python. Apparently
|
||||
some of HP/Compaq’s administrative tools are written in Python.</p></li>
|
||||
<li><p>Many Unix-compatible operating systems, such as Mac OS X and some Linux
|
||||
distributions, have Python installed by default; it’s included in the base
|
||||
installation.</p></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section" id="can-i-delete-python">
|
||||
<h2>Can I delete Python?<a class="headerlink" href="#can-i-delete-python" title="Permalink to this headline">¶</a></h2>
|
||||
<p>That depends on where Python came from.</p>
|
||||
<p>If someone installed it deliberately, you can remove it without hurting
|
||||
anything. On Windows, use the Add/Remove Programs icon in the Control Panel.</p>
|
||||
<p>If Python was installed by a third-party application, you can also remove it,
|
||||
but that application will no longer work. You should use that application’s
|
||||
uninstaller rather than removing Python directly.</p>
|
||||
<p>If Python came with your operating system, removing it is not recommended. If
|
||||
you remove it, whatever tools were written in Python will no longer run, and
|
||||
some of them might be important to you. Reinstalling the whole system would
|
||||
then be required to fix things again.</p>
|
||||
</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="#">“Why is Python Installed on my Computer?” FAQ</a><ul>
|
||||
<li><a class="reference internal" href="#what-is-python">What is Python?</a></li>
|
||||
<li><a class="reference internal" href="#why-is-python-installed-on-my-machine">Why is Python installed on my machine?</a></li>
|
||||
<li><a class="reference internal" href="#can-i-delete-python">Can I delete Python?</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>Previous topic</h4>
|
||||
<p class="topless"><a href="gui.html"
|
||||
title="previous chapter">Graphic User Interface FAQ</a></p>
|
||||
<h4>Next topic</h4>
|
||||
<p class="topless"><a href="../glossary.html"
|
||||
title="next chapter">Glossary</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/installed.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="../glossary.html" title="Glossary"
|
||||
>next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="gui.html" title="Graphic User Interface 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>
|
865
python-3.7.4-docs-html/faq/library.html
Normal file
865
python-3.7.4-docs-html/faq/library.html
Normal file
@@ -0,0 +1,865 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Library and Extension 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="Extending/Embedding FAQ" href="extending.html" />
|
||||
<link rel="prev" title="Design and History FAQ" href="design.html" />
|
||||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/faq/library.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="extending.html" title="Extending/Embedding FAQ"
|
||||
accesskey="N">next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="design.html" title="Design and History 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="library-and-extension-faq">
|
||||
<h1><a class="toc-backref" href="#id1">Library and Extension FAQ</a><a class="headerlink" href="#library-and-extension-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="#library-and-extension-faq" id="id1">Library and Extension FAQ</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#general-library-questions" id="id2">General Library Questions</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#how-do-i-find-a-module-or-application-to-perform-task-x" id="id3">How do I find a module or application to perform task X?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#where-is-the-math-py-socket-py-regex-py-etc-source-file" id="id4">Where is the math.py (socket.py, regex.py, etc.) source file?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-make-a-python-script-executable-on-unix" id="id5">How do I make a Python script executable on Unix?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#is-there-a-curses-termcap-package-for-python" id="id6">Is there a curses/termcap package for Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#is-there-an-equivalent-to-c-s-onexit-in-python" id="id7">Is there an equivalent to C’s onexit() in Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-don-t-my-signal-handlers-work" id="id8">Why don’t my signal handlers work?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="#common-tasks" id="id9">Common tasks</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#how-do-i-test-a-python-program-or-component" id="id10">How do I test a Python program or component?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-create-documentation-from-doc-strings" id="id11">How do I create documentation from doc strings?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-get-a-single-keypress-at-a-time" id="id12">How do I get a single keypress at a time?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="#threads" id="id13">Threads</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#how-do-i-program-using-threads" id="id14">How do I program using threads?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#none-of-my-threads-seem-to-run-why" id="id15">None of my threads seem to run: why?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-parcel-out-work-among-a-bunch-of-worker-threads" id="id16">How do I parcel out work among a bunch of worker threads?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#what-kinds-of-global-value-mutation-are-thread-safe" id="id17">What kinds of global value mutation are thread-safe?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#can-t-we-get-rid-of-the-global-interpreter-lock" id="id18">Can’t we get rid of the Global Interpreter Lock?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="#input-and-output" id="id19">Input and Output</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#how-do-i-delete-a-file-and-other-file-questions" id="id20">How do I delete a file? (And other file questions…)</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-copy-a-file" id="id21">How do I copy a file?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-read-or-write-binary-data" id="id22">How do I read (or write) binary data?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#i-can-t-seem-to-use-os-read-on-a-pipe-created-with-os-popen-why" id="id23">I can’t seem to use os.read() on a pipe created with os.popen(); why?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-access-the-serial-rs232-port" id="id24">How do I access the serial (RS232) port?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-doesn-t-closing-sys-stdout-stdin-stderr-really-close-it" id="id25">Why doesn’t closing sys.stdout (stdin, stderr) really close it?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="#network-internet-programming" id="id26">Network/Internet Programming</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#what-www-tools-are-there-for-python" id="id27">What WWW tools are there for Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-can-i-mimic-cgi-form-submission-method-post" id="id28">How can I mimic CGI form submission (METHOD=POST)?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#what-module-should-i-use-to-help-with-generating-html" id="id29">What module should I use to help with generating HTML?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-send-mail-from-a-python-script" id="id30">How do I send mail from a Python script?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-avoid-blocking-in-the-connect-method-of-a-socket" id="id31">How do I avoid blocking in the connect() method of a socket?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="#databases" id="id32">Databases</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#are-there-any-interfaces-to-database-packages-in-python" id="id33">Are there any interfaces to database packages in Python?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-you-implement-persistent-objects-in-python" id="id34">How do you implement persistent objects in Python?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="#mathematics-and-numerics" id="id35">Mathematics and Numerics</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#how-do-i-generate-random-numbers-in-python" id="id36">How do I generate random numbers in Python?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section" id="general-library-questions">
|
||||
<h2><a class="toc-backref" href="#id2">General Library Questions</a><a class="headerlink" href="#general-library-questions" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="how-do-i-find-a-module-or-application-to-perform-task-x">
|
||||
<h3><a class="toc-backref" href="#id3">How do I find a module or application to perform task X?</a><a class="headerlink" href="#how-do-i-find-a-module-or-application-to-perform-task-x" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Check <a class="reference internal" href="../library/index.html#library-index"><span class="std std-ref">the Library Reference</span></a> to see if there’s a relevant
|
||||
standard library module. (Eventually you’ll learn what’s in the standard
|
||||
library and will be able to skip this step.)</p>
|
||||
<p>For third-party packages, search the <a class="reference external" href="https://pypi.org">Python Package Index</a> or try <a class="reference external" href="https://www.google.com">Google</a> or
|
||||
another Web search engine. Searching for “Python” plus a keyword or two for
|
||||
your topic of interest will usually find something helpful.</p>
|
||||
</div>
|
||||
<div class="section" id="where-is-the-math-py-socket-py-regex-py-etc-source-file">
|
||||
<h3><a class="toc-backref" href="#id4">Where is the math.py (socket.py, regex.py, etc.) source file?</a><a class="headerlink" href="#where-is-the-math-py-socket-py-regex-py-etc-source-file" title="Permalink to this headline">¶</a></h3>
|
||||
<p>If you can’t find a source file for a module it may be a built-in or
|
||||
dynamically loaded module implemented in C, C++ or other compiled language.
|
||||
In this case you may not have the source file or it may be something like
|
||||
<code class="file docutils literal notranslate"><span class="pre">mathmodule.c</span></code>, somewhere in a C source directory (not on the Python Path).</p>
|
||||
<p>There are (at least) three kinds of modules in Python:</p>
|
||||
<ol class="arabic">
|
||||
<li><p>modules written in Python (.py);</p></li>
|
||||
<li><p>modules written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc);</p></li>
|
||||
<li><p>modules written in C and linked with the interpreter; to get a list of these,
|
||||
type:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">builtin_module_names</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</li>
|
||||
</ol>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-make-a-python-script-executable-on-unix">
|
||||
<h3><a class="toc-backref" href="#id5">How do I make a Python script executable on Unix?</a><a class="headerlink" href="#how-do-i-make-a-python-script-executable-on-unix" title="Permalink to this headline">¶</a></h3>
|
||||
<p>You need to do two things: the script file’s mode must be executable and the
|
||||
first line must begin with <code class="docutils literal notranslate"><span class="pre">#!</span></code> followed by the path of the Python
|
||||
interpreter.</p>
|
||||
<p>The first is done by executing <code class="docutils literal notranslate"><span class="pre">chmod</span> <span class="pre">+x</span> <span class="pre">scriptfile</span></code> or perhaps <code class="docutils literal notranslate"><span class="pre">chmod</span> <span class="pre">755</span>
|
||||
<span class="pre">scriptfile</span></code>.</p>
|
||||
<p>The second can be done in a number of ways. The most straightforward way is to
|
||||
write</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/local/bin/python</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>as the very first line of your file, using the pathname for where the Python
|
||||
interpreter is installed on your platform.</p>
|
||||
<p>If you would like the script to be independent of where the Python interpreter
|
||||
lives, you can use the <strong class="program">env</strong> program. Almost all Unix variants support
|
||||
the following, assuming the Python interpreter is in a directory on the user’s
|
||||
<span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p><em>Don’t</em> do this for CGI scripts. The <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> variable for CGI scripts is
|
||||
often very minimal, so you need to use the actual absolute pathname of the
|
||||
interpreter.</p>
|
||||
<p>Occasionally, a user’s environment is so full that the <strong class="program">/usr/bin/env</strong>
|
||||
program fails; or there’s no env program at all. In that case, you can try the
|
||||
following hack (due to Alex Rezinsky):</p>
|
||||
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="ch">#! /bin/sh</span>
|
||||
<span class="s2">""":"</span>
|
||||
<span class="nb">exec</span> python <span class="nv">$0</span> <span class="si">${</span><span class="nv">1</span><span class="p">+</span><span class="s2">"</span><span class="nv">$@</span><span class="s2">"</span><span class="si">}</span>
|
||||
<span class="s2">"""</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The minor disadvantage is that this defines the script’s __doc__ string.
|
||||
However, you can fix that by adding</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="vm">__doc__</span> <span class="o">=</span> <span class="s2">"""...Whatever..."""</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="is-there-a-curses-termcap-package-for-python">
|
||||
<h3><a class="toc-backref" href="#id6">Is there a curses/termcap package for Python?</a><a class="headerlink" href="#is-there-a-curses-termcap-package-for-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>For Unix variants: The standard Python source distribution comes with a curses
|
||||
module in the <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Modules">Modules</a> subdirectory, though it’s not compiled by default.
|
||||
(Note that this is not available in the Windows distribution – there is no
|
||||
curses module for Windows.)</p>
|
||||
<p>The <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a> module supports basic curses features as well as many additional
|
||||
functions from ncurses and SYSV curses such as colour, alternative character set
|
||||
support, pads, and mouse support. This means the module isn’t compatible with
|
||||
operating systems that only have BSD curses, but there don’t seem to be any
|
||||
currently maintained OSes that fall into this category.</p>
|
||||
<p>For Windows: use <a class="reference external" href="http://effbot.org/zone/console-index.htm">the consolelib module</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="is-there-an-equivalent-to-c-s-onexit-in-python">
|
||||
<h3><a class="toc-backref" href="#id7">Is there an equivalent to C’s onexit() in Python?</a><a class="headerlink" href="#is-there-an-equivalent-to-c-s-onexit-in-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The <a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a> module provides a register function that is similar to C’s
|
||||
<code class="xref c c-func docutils literal notranslate"><span class="pre">onexit()</span></code>.</p>
|
||||
</div>
|
||||
<div class="section" id="why-don-t-my-signal-handlers-work">
|
||||
<h3><a class="toc-backref" href="#id8">Why don’t my signal handlers work?</a><a class="headerlink" href="#why-don-t-my-signal-handlers-work" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The most common problem is that the signal handler is declared with the wrong
|
||||
argument list. It is called as</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">handler</span><span class="p">(</span><span class="n">signum</span><span class="p">,</span> <span class="n">frame</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>so it should be declared with two arguments:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">handler</span><span class="p">(</span><span class="n">signum</span><span class="p">,</span> <span class="n">frame</span><span class="p">):</span>
|
||||
<span class="o">...</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="common-tasks">
|
||||
<h2><a class="toc-backref" href="#id9">Common tasks</a><a class="headerlink" href="#common-tasks" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="how-do-i-test-a-python-program-or-component">
|
||||
<h3><a class="toc-backref" href="#id10">How do I test a Python program or component?</a><a class="headerlink" href="#how-do-i-test-a-python-program-or-component" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Python comes with two testing frameworks. The <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> module finds
|
||||
examples in the docstrings for a module and runs them, comparing the output with
|
||||
the expected output given in the docstring.</p>
|
||||
<p>The <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> module is a fancier testing framework modelled on Java and
|
||||
Smalltalk testing frameworks.</p>
|
||||
<p>To make testing easier, you should use good modular design in your program.
|
||||
Your program should have almost all functionality
|
||||
encapsulated in either functions or class methods – and this sometimes has the
|
||||
surprising and delightful effect of making the program run faster (because local
|
||||
variable accesses are faster than global accesses). Furthermore the program
|
||||
should avoid depending on mutating global variables, since this makes testing
|
||||
much more difficult to do.</p>
|
||||
<p>The “global main logic” of your program may be as simple as</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
|
||||
<span class="n">main_logic</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>at the bottom of the main module of your program.</p>
|
||||
<p>Once your program is organized as a tractable collection of functions and class
|
||||
behaviours you should write test functions that exercise the behaviours. A test
|
||||
suite that automates a sequence of tests can be associated with each module.
|
||||
This sounds like a lot of work, but since Python is so terse and flexible it’s
|
||||
surprisingly easy. You can make coding much more pleasant and fun by writing
|
||||
your test functions in parallel with the “production code”, since this makes it
|
||||
easy to find bugs and even design flaws earlier.</p>
|
||||
<p>“Support modules” that are not intended to be the main module of a program may
|
||||
include a self-test of the module.</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
|
||||
<span class="n">self_test</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Even programs that interact with complex external interfaces may be tested when
|
||||
the external interfaces are unavailable by using “fake” interfaces implemented
|
||||
in Python.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-create-documentation-from-doc-strings">
|
||||
<h3><a class="toc-backref" href="#id11">How do I create documentation from doc strings?</a><a class="headerlink" href="#how-do-i-create-documentation-from-doc-strings" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The <a class="reference internal" href="../library/pydoc.html#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> module can create HTML from the doc strings in your Python
|
||||
source code. An alternative for creating API documentation purely from
|
||||
docstrings is <a class="reference external" href="http://epydoc.sourceforge.net/">epydoc</a>. <a class="reference external" href="http://sphinx-doc.org">Sphinx</a> can also include docstring content.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-get-a-single-keypress-at-a-time">
|
||||
<h3><a class="toc-backref" href="#id12">How do I get a single keypress at a time?</a><a class="headerlink" href="#how-do-i-get-a-single-keypress-at-a-time" title="Permalink to this headline">¶</a></h3>
|
||||
<p>For Unix variants there are several solutions. It’s straightforward to do this
|
||||
using curses, but curses is a fairly large module to learn.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="threads">
|
||||
<h2><a class="toc-backref" href="#id13">Threads</a><a class="headerlink" href="#threads" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="how-do-i-program-using-threads">
|
||||
<h3><a class="toc-backref" href="#id14">How do I program using threads?</a><a class="headerlink" href="#how-do-i-program-using-threads" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Be sure to use the <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module and not the <a class="reference internal" href="../library/_thread.html#module-_thread" title="_thread: Low-level threading API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">_thread</span></code></a> module.
|
||||
The <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module builds convenient abstractions on top of the
|
||||
low-level primitives provided by the <a class="reference internal" href="../library/_thread.html#module-_thread" title="_thread: Low-level threading API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">_thread</span></code></a> module.</p>
|
||||
<p>Aahz has a set of slides from his threading tutorial that are helpful; see
|
||||
<a class="reference external" href="http://www.pythoncraft.com/OSCON2001/">http://www.pythoncraft.com/OSCON2001/</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="none-of-my-threads-seem-to-run-why">
|
||||
<h3><a class="toc-backref" href="#id15">None of my threads seem to run: why?</a><a class="headerlink" href="#none-of-my-threads-seem-to-run-why" title="Permalink to this headline">¶</a></h3>
|
||||
<p>As soon as the main thread exits, all threads are killed. Your main thread is
|
||||
running too quickly, giving the threads no time to do any work.</p>
|
||||
<p>A simple fix is to add a sleep to the end of the program that’s long enough for
|
||||
all the threads to finish:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">threading</span><span class="o">,</span> <span class="nn">time</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">thread_task</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">n</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="n">n</span><span class="p">):</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">i</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="mi">10</span><span class="p">):</span>
|
||||
<span class="n">T</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">thread_task</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">),</span> <span class="n">i</span><span class="p">))</span>
|
||||
<span class="n">T</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
|
||||
|
||||
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c1"># <---------------------------!</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>But now (on many platforms) the threads don’t run in parallel, but appear to run
|
||||
sequentially, one at a time! The reason is that the OS thread scheduler doesn’t
|
||||
start a new thread until the previous thread is blocked.</p>
|
||||
<p>A simple fix is to add a tiny sleep to the start of the run function:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">thread_task</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
|
||||
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.001</span><span class="p">)</span> <span class="c1"># <--------------------!</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">n</span><span class="p">):</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">i</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="mi">10</span><span class="p">):</span>
|
||||
<span class="n">T</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">thread_task</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">),</span> <span class="n">i</span><span class="p">))</span>
|
||||
<span class="n">T</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
|
||||
|
||||
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Instead of trying to guess a good delay value for <a class="reference internal" href="../library/time.html#time.sleep" title="time.sleep"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.sleep()</span></code></a>,
|
||||
it’s better to use some kind of semaphore mechanism. One idea is to use the
|
||||
<a class="reference internal" href="../library/queue.html#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a> module to create a queue object, let each thread append a token to
|
||||
the queue when it finishes, and let the main thread read as many tokens from the
|
||||
queue as there are threads.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-parcel-out-work-among-a-bunch-of-worker-threads">
|
||||
<h3><a class="toc-backref" href="#id16">How do I parcel out work among a bunch of worker threads?</a><a class="headerlink" href="#how-do-i-parcel-out-work-among-a-bunch-of-worker-threads" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The easiest way is to use the new <a class="reference internal" href="../library/concurrent.futures.html#module-concurrent.futures" title="concurrent.futures: Execute computations concurrently using threads or processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> module,
|
||||
especially the <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a> class.</p>
|
||||
<p>Or, if you want fine control over the dispatching algorithm, you can write
|
||||
your own logic manually. Use the <a class="reference internal" href="../library/queue.html#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a> module to create a queue
|
||||
containing a list of jobs. The <a class="reference internal" href="../library/queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> class maintains a
|
||||
list of objects and has a <code class="docutils literal notranslate"><span class="pre">.put(obj)</span></code> method that adds items to the queue and
|
||||
a <code class="docutils literal notranslate"><span class="pre">.get()</span></code> method to return them. The class will take care of the locking
|
||||
necessary to ensure that each job is handed out exactly once.</p>
|
||||
<p>Here’s a trivial example:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">threading</span><span class="o">,</span> <span class="nn">queue</span><span class="o">,</span> <span class="nn">time</span>
|
||||
|
||||
<span class="c1"># The worker thread gets jobs off the queue. When the queue is empty, it</span>
|
||||
<span class="c1"># assumes there will be no more work and exits.</span>
|
||||
<span class="c1"># (Realistically workers will run until terminated.)</span>
|
||||
<span class="k">def</span> <span class="nf">worker</span><span class="p">():</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Running worker'</span><span class="p">)</span>
|
||||
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
|
||||
<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
|
||||
<span class="k">try</span><span class="p">:</span>
|
||||
<span class="n">arg</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">block</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
|
||||
<span class="k">except</span> <span class="n">queue</span><span class="o">.</span><span class="n">Empty</span><span class="p">:</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Worker'</span><span class="p">,</span> <span class="n">threading</span><span class="o">.</span><span class="n">currentThread</span><span class="p">(),</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s1">'queue empty'</span><span class="p">)</span>
|
||||
<span class="k">break</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Worker'</span><span class="p">,</span> <span class="n">threading</span><span class="o">.</span><span class="n">currentThread</span><span class="p">(),</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s1">'running with argument'</span><span class="p">,</span> <span class="n">arg</span><span class="p">)</span>
|
||||
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span>
|
||||
|
||||
<span class="c1"># Create queue</span>
|
||||
<span class="n">q</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">Queue</span><span class="p">()</span>
|
||||
|
||||
<span class="c1"># Start a pool of 5 workers</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">5</span><span class="p">):</span>
|
||||
<span class="n">t</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">worker</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'worker </span><span class="si">%i</span><span class="s1">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span>
|
||||
<span class="n">t</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
|
||||
|
||||
<span class="c1"># Begin adding work to the queue</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">50</span><span class="p">):</span>
|
||||
<span class="n">q</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||
|
||||
<span class="c1"># Give threads time to run</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Main thread sleeping'</span><span class="p">)</span>
|
||||
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>When run, this will produce the following output:</p>
|
||||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Running worker
|
||||
Running worker
|
||||
Running worker
|
||||
Running worker
|
||||
Running worker
|
||||
Main thread sleeping
|
||||
Worker <Thread(worker 1, started 130283832797456)> running with argument 0
|
||||
Worker <Thread(worker 2, started 130283824404752)> running with argument 1
|
||||
Worker <Thread(worker 3, started 130283816012048)> running with argument 2
|
||||
Worker <Thread(worker 4, started 130283807619344)> running with argument 3
|
||||
Worker <Thread(worker 5, started 130283799226640)> running with argument 4
|
||||
Worker <Thread(worker 1, started 130283832797456)> running with argument 5
|
||||
...
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Consult the module’s documentation for more details; the <a class="reference internal" href="../library/queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a>
|
||||
class provides a featureful interface.</p>
|
||||
</div>
|
||||
<div class="section" id="what-kinds-of-global-value-mutation-are-thread-safe">
|
||||
<h3><a class="toc-backref" href="#id17">What kinds of global value mutation are thread-safe?</a><a class="headerlink" href="#what-kinds-of-global-value-mutation-are-thread-safe" title="Permalink to this headline">¶</a></h3>
|
||||
<p>A <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">global interpreter lock</span></a> (GIL) is used internally to ensure that only one
|
||||
thread runs in the Python VM at a time. In general, Python offers to switch
|
||||
among threads only between bytecode instructions; how frequently it switches can
|
||||
be set via <a class="reference internal" href="../library/sys.html#sys.setswitchinterval" title="sys.setswitchinterval"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setswitchinterval()</span></code></a>. Each bytecode instruction and
|
||||
therefore all the C implementation code reached from each instruction is
|
||||
therefore atomic from the point of view of a Python program.</p>
|
||||
<p>In theory, this means an exact accounting requires an exact understanding of the
|
||||
PVM bytecode implementation. In practice, it means that operations on shared
|
||||
variables of built-in data types (ints, lists, dicts, etc) that “look atomic”
|
||||
really are.</p>
|
||||
<p>For example, the following operations are all atomic (L, L1, L2 are lists, D,
|
||||
D1, D2 are dicts, x, y are objects, i, j are ints):</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">L</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
|
||||
<span class="n">L1</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">L2</span><span class="p">)</span>
|
||||
<span class="n">x</span> <span class="o">=</span> <span class="n">L</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
|
||||
<span class="n">x</span> <span class="o">=</span> <span class="n">L</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
|
||||
<span class="n">L1</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">L2</span>
|
||||
<span class="n">L</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
|
||||
<span class="n">x</span> <span class="o">=</span> <span class="n">y</span>
|
||||
<span class="n">x</span><span class="o">.</span><span class="n">field</span> <span class="o">=</span> <span class="n">y</span>
|
||||
<span class="n">D</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">y</span>
|
||||
<span class="n">D1</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">D2</span><span class="p">)</span>
|
||||
<span class="n">D</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>These aren’t:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">i</span> <span class="o">=</span> <span class="n">i</span><span class="o">+</span><span class="mi">1</span>
|
||||
<span class="n">L</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">L</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
|
||||
<span class="n">L</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">L</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
|
||||
<span class="n">D</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">D</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Operations that replace other objects may invoke those other objects’
|
||||
<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 when their reference count reaches zero, and that can
|
||||
affect things. This is especially true for the mass updates to dictionaries and
|
||||
lists. When in doubt, use a mutex!</p>
|
||||
</div>
|
||||
<div class="section" id="can-t-we-get-rid-of-the-global-interpreter-lock">
|
||||
<h3><a class="toc-backref" href="#id18">Can’t we get rid of the Global Interpreter Lock?</a><a class="headerlink" href="#can-t-we-get-rid-of-the-global-interpreter-lock" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">global interpreter lock</span></a> (GIL) is often seen as a hindrance to Python’s
|
||||
deployment on high-end multiprocessor server machines, because a multi-threaded
|
||||
Python program effectively only uses one CPU, due to the insistence that
|
||||
(almost) all Python code can only run while the GIL is held.</p>
|
||||
<p>Back in the days of Python 1.5, Greg Stein actually implemented a comprehensive
|
||||
patch set (the “free threading” patches) that removed the GIL and replaced it
|
||||
with fine-grained locking. Adam Olsen recently did a similar experiment
|
||||
in his <a class="reference external" href="https://code.google.com/archive/p/python-safethread">python-safethread</a>
|
||||
project. Unfortunately, both experiments exhibited a sharp drop in single-thread
|
||||
performance (at least 30% slower), due to the amount of fine-grained locking
|
||||
necessary to compensate for the removal of the GIL.</p>
|
||||
<p>This doesn’t mean that you can’t make good use of Python on multi-CPU machines!
|
||||
You just have to be creative with dividing the work up between multiple
|
||||
<em>processes</em> rather than multiple <em>threads</em>. The
|
||||
<a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a> class in the new
|
||||
<a class="reference internal" href="../library/concurrent.futures.html#module-concurrent.futures" title="concurrent.futures: Execute computations concurrently using threads or processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> module provides an easy way of doing so; the
|
||||
<a class="reference internal" href="../library/multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> module provides a lower-level API in case you want
|
||||
more control over dispatching of tasks.</p>
|
||||
<p>Judicious use of C extensions will also help; if you use a C extension to
|
||||
perform a time-consuming task, the extension can release the GIL while the
|
||||
thread of execution is in the C code and allow other threads to get some work
|
||||
done. Some standard library modules such as <a class="reference internal" href="../library/zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> and <a class="reference internal" href="../library/hashlib.html#module-hashlib" title="hashlib: Secure hash and message digest algorithms."><code class="xref py py-mod docutils literal notranslate"><span class="pre">hashlib</span></code></a>
|
||||
already do this.</p>
|
||||
<p>It has been suggested that the GIL should be a per-interpreter-state lock rather
|
||||
than truly global; interpreters then wouldn’t be able to share objects.
|
||||
Unfortunately, this isn’t likely to happen either. It would be a tremendous
|
||||
amount of work, because many object implementations currently have global state.
|
||||
For example, small integers and short strings are cached; these caches would
|
||||
have to be moved to the interpreter state. Other object types have their own
|
||||
free list; these free lists would have to be moved to the interpreter state.
|
||||
And so on.</p>
|
||||
<p>And I doubt that it can even be done in finite time, because the same problem
|
||||
exists for 3rd party extensions. It is likely that 3rd party extensions are
|
||||
being written at a faster rate than you can convert them to store all their
|
||||
global state in the interpreter state.</p>
|
||||
<p>And finally, once you have multiple interpreters not sharing any state, what
|
||||
have you gained over running each interpreter in a separate process?</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="input-and-output">
|
||||
<h2><a class="toc-backref" href="#id19">Input and Output</a><a class="headerlink" href="#input-and-output" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="how-do-i-delete-a-file-and-other-file-questions">
|
||||
<h3><a class="toc-backref" href="#id20">How do I delete a file? (And other file questions…)</a><a class="headerlink" href="#how-do-i-delete-a-file-and-other-file-questions" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Use <code class="docutils literal notranslate"><span class="pre">os.remove(filename)</span></code> or <code class="docutils literal notranslate"><span class="pre">os.unlink(filename)</span></code>; for documentation, see
|
||||
the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module. The two functions are identical; <a class="reference internal" href="../library/os.html#os.unlink" title="os.unlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">unlink()</span></code></a> is simply
|
||||
the name of the Unix system call for this function.</p>
|
||||
<p>To remove a directory, use <a class="reference internal" href="../library/os.html#os.rmdir" title="os.rmdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.rmdir()</span></code></a>; use <a class="reference internal" href="../library/os.html#os.mkdir" title="os.mkdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.mkdir()</span></code></a> to create one.
|
||||
<code class="docutils literal notranslate"><span class="pre">os.makedirs(path)</span></code> will create any intermediate directories in <code class="docutils literal notranslate"><span class="pre">path</span></code> that
|
||||
don’t exist. <code class="docutils literal notranslate"><span class="pre">os.removedirs(path)</span></code> will remove intermediate directories as
|
||||
long as they’re empty; if you want to delete an entire directory tree and its
|
||||
contents, use <a class="reference internal" href="../library/shutil.html#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.rmtree()</span></code></a>.</p>
|
||||
<p>To rename a file, use <code class="docutils literal notranslate"><span class="pre">os.rename(old_path,</span> <span class="pre">new_path)</span></code>.</p>
|
||||
<p>To truncate a file, open it using <code class="docutils literal notranslate"><span class="pre">f</span> <span class="pre">=</span> <span class="pre">open(filename,</span> <span class="pre">"rb+")</span></code>, and use
|
||||
<code class="docutils literal notranslate"><span class="pre">f.truncate(offset)</span></code>; offset defaults to the current seek position. There’s
|
||||
also <code class="docutils literal notranslate"><span class="pre">os.ftruncate(fd,</span> <span class="pre">offset)</span></code> for files opened with <a class="reference internal" href="../library/os.html#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a>, where
|
||||
<em>fd</em> is the file descriptor (a small integer).</p>
|
||||
<p>The <a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a> module also contains a number of functions to work on files
|
||||
including <a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">copyfile()</span></code></a>, <a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">copytree()</span></code></a>, and
|
||||
<a class="reference internal" href="../library/shutil.html#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal notranslate"><span class="pre">rmtree()</span></code></a>.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-copy-a-file">
|
||||
<h3><a class="toc-backref" href="#id21">How do I copy a file?</a><a class="headerlink" href="#how-do-i-copy-a-file" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The <a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a> module contains a <a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">copyfile()</span></code></a> function. Note
|
||||
that on MacOS 9 it doesn’t copy the resource fork and Finder info.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-read-or-write-binary-data">
|
||||
<h3><a class="toc-backref" href="#id22">How do I read (or write) binary data?</a><a class="headerlink" href="#how-do-i-read-or-write-binary-data" title="Permalink to this headline">¶</a></h3>
|
||||
<p>To read or write complex binary data formats, it’s best to use the <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a>
|
||||
module. It allows you to take a string containing binary data (usually numbers)
|
||||
and convert it to Python objects; and vice versa.</p>
|
||||
<p>For example, the following code reads two 2-byte integers and one 4-byte integer
|
||||
in big-endian format from a file:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">struct</span>
|
||||
|
||||
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s2">"rb"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
|
||||
<span class="n">s</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">8</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">z</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s2">">hhl"</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The ‘>’ in the format string forces big-endian data; the letter ‘h’ reads one
|
||||
“short integer” (2 bytes), and ‘l’ reads one “long integer” (4 bytes) from the
|
||||
string.</p>
|
||||
<p>For data that is more regular (e.g. a homogeneous list of ints or floats),
|
||||
you can also use 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="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<p>To read and write binary data, it is mandatory to open the file in
|
||||
binary mode (here, passing <code class="docutils literal notranslate"><span class="pre">"rb"</span></code> to <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>). If you use
|
||||
<code class="docutils literal notranslate"><span class="pre">"r"</span></code> instead (the default), the file will be open in text mode
|
||||
and <code class="docutils literal notranslate"><span class="pre">f.read()</span></code> will return <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 rather than
|
||||
<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.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="i-can-t-seem-to-use-os-read-on-a-pipe-created-with-os-popen-why">
|
||||
<h3><a class="toc-backref" href="#id23">I can’t seem to use os.read() on a pipe created with os.popen(); why?</a><a class="headerlink" href="#i-can-t-seem-to-use-os-read-on-a-pipe-created-with-os-popen-why" title="Permalink to this headline">¶</a></h3>
|
||||
<p><a class="reference internal" href="../library/os.html#os.read" title="os.read"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.read()</span></code></a> is a low-level function which takes a file descriptor, a small
|
||||
integer representing the opened file. <a class="reference internal" href="../library/os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen()</span></code></a> creates a high-level
|
||||
file object, the same type returned by the built-in <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function.
|
||||
Thus, to read <em>n</em> bytes from a pipe <em>p</em> created with <a class="reference internal" href="../library/os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen()</span></code></a>, you need to
|
||||
use <code class="docutils literal notranslate"><span class="pre">p.read(n)</span></code>.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-access-the-serial-rs232-port">
|
||||
<h3><a class="toc-backref" href="#id24">How do I access the serial (RS232) port?</a><a class="headerlink" href="#how-do-i-access-the-serial-rs232-port" title="Permalink to this headline">¶</a></h3>
|
||||
<p>For Win32, POSIX (Linux, BSD, etc.), Jython:</p>
|
||||
<blockquote>
|
||||
<div><p><a class="reference external" href="http://pyserial.sourceforge.net">http://pyserial.sourceforge.net</a></p>
|
||||
</div></blockquote>
|
||||
<p>For Unix, see a Usenet post by Mitch Chapman:</p>
|
||||
<blockquote>
|
||||
<div><p><a class="reference external" href="https://groups.google.com/groups?selm=34A04430.CF9@ohioee.com">https://groups.google.com/groups?selm=34A04430.CF9@ohioee.com</a></p>
|
||||
</div></blockquote>
|
||||
</div>
|
||||
<div class="section" id="why-doesn-t-closing-sys-stdout-stdin-stderr-really-close-it">
|
||||
<h3><a class="toc-backref" href="#id25">Why doesn’t closing sys.stdout (stdin, stderr) really close it?</a><a class="headerlink" href="#why-doesn-t-closing-sys-stdout-stdin-stderr-really-close-it" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Python <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file objects</span></a> are a high-level layer of
|
||||
abstraction on low-level C file descriptors.</p>
|
||||
<p>For most file objects you create in Python via the built-in <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>
|
||||
function, <code class="docutils literal notranslate"><span class="pre">f.close()</span></code> marks the Python file object as being closed from
|
||||
Python’s point of view, and also arranges to close the underlying C file
|
||||
descriptor. This also happens automatically in <code class="docutils literal notranslate"><span class="pre">f</span></code>’s destructor, when
|
||||
<code class="docutils literal notranslate"><span class="pre">f</span></code> becomes garbage.</p>
|
||||
<p>But stdin, stdout and stderr are treated specially by Python, because of the
|
||||
special status also given to them by C. Running <code class="docutils literal notranslate"><span class="pre">sys.stdout.close()</span></code> marks
|
||||
the Python-level file object as being closed, but does <em>not</em> close the
|
||||
associated C file descriptor.</p>
|
||||
<p>To close the underlying C file descriptor for one of these three, you should
|
||||
first be sure that’s what you really want to do (e.g., you may confuse
|
||||
extension modules trying to do I/O). If it is, use <a class="reference internal" href="../library/os.html#os.close" title="os.close"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.close()</span></code></a>:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">stdin</span><span class="o">.</span><span class="n">fileno</span><span class="p">())</span>
|
||||
<span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">stdout</span><span class="o">.</span><span class="n">fileno</span><span class="p">())</span>
|
||||
<span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">stderr</span><span class="o">.</span><span class="n">fileno</span><span class="p">())</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Or you can use the numeric constants 0, 1 and 2, respectively.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="network-internet-programming">
|
||||
<h2><a class="toc-backref" href="#id26">Network/Internet Programming</a><a class="headerlink" href="#network-internet-programming" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="what-www-tools-are-there-for-python">
|
||||
<h3><a class="toc-backref" href="#id27">What WWW tools are there for Python?</a><a class="headerlink" href="#what-www-tools-are-there-for-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>See the chapters titled <a class="reference internal" href="../library/internet.html#internet"><span class="std std-ref">Internet Protocols and Support</span></a> and <a class="reference internal" href="../library/netdata.html#netdata"><span class="std std-ref">Internet Data Handling</span></a> in the Library
|
||||
Reference Manual. Python has many modules that will help you build server-side
|
||||
and client-side web systems.</p>
|
||||
<p>A summary of available frameworks is maintained by Paul Boddie at
|
||||
<a class="reference external" href="https://wiki.python.org/moin/WebProgramming">https://wiki.python.org/moin/WebProgramming</a>.</p>
|
||||
<p>Cameron Laird maintains a useful set of pages about Python web technologies at
|
||||
<a class="reference external" href="http://phaseit.net/claird/comp.lang.python/web_python">http://phaseit.net/claird/comp.lang.python/web_python</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="how-can-i-mimic-cgi-form-submission-method-post">
|
||||
<h3><a class="toc-backref" href="#id28">How can I mimic CGI form submission (METHOD=POST)?</a><a class="headerlink" href="#how-can-i-mimic-cgi-form-submission-method-post" title="Permalink to this headline">¶</a></h3>
|
||||
<p>I would like to retrieve web pages that are the result of POSTing a form. Is
|
||||
there existing code that would let me do this easily?</p>
|
||||
<p>Yes. Here’s a simple example that uses urllib.request:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/local/bin/python</span>
|
||||
|
||||
<span class="kn">import</span> <span class="nn">urllib.request</span>
|
||||
|
||||
<span class="c1"># build the query string</span>
|
||||
<span class="n">qs</span> <span class="o">=</span> <span class="s2">"First=Josephine&MI=Q&Last=Public"</span>
|
||||
|
||||
<span class="c1"># connect and send the server a path</span>
|
||||
<span class="n">req</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s1">'http://www.some-server.out-there'</span>
|
||||
<span class="s1">'/cgi-bin/some-cgi-script'</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">qs</span><span class="p">)</span>
|
||||
<span class="k">with</span> <span class="n">req</span><span class="p">:</span>
|
||||
<span class="n">msg</span><span class="p">,</span> <span class="n">hdrs</span> <span class="o">=</span> <span class="n">req</span><span class="o">.</span><span class="n">read</span><span class="p">(),</span> <span class="n">req</span><span class="o">.</span><span class="n">info</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Note that in general for percent-encoded POST operations, query strings must be
|
||||
quoted using <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urlencode" title="urllib.parse.urlencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">urllib.parse.urlencode()</span></code></a>. For example, to send
|
||||
<code class="docutils literal notranslate"><span class="pre">name=Guy</span> <span class="pre">Steele,</span> <span class="pre">Jr.</span></code>:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">urllib.parse</span>
|
||||
<span class="gp">>>> </span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">({</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'Guy Steele, Jr.'</span><span class="p">})</span>
|
||||
<span class="go">'name=Guy+Steele%2C+Jr.'</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">See also</p>
|
||||
<p><a class="reference internal" href="../howto/urllib2.html#urllib-howto"><span class="std std-ref">HOWTO Fetch Internet Resources Using The urllib Package</span></a> for extensive examples.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="what-module-should-i-use-to-help-with-generating-html">
|
||||
<h3><a class="toc-backref" href="#id29">What module should I use to help with generating HTML?</a><a class="headerlink" href="#what-module-should-i-use-to-help-with-generating-html" title="Permalink to this headline">¶</a></h3>
|
||||
<p>You can find a collection of useful links on the <a class="reference external" href="https://wiki.python.org/moin/WebProgramming">Web Programming wiki page</a>.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-send-mail-from-a-python-script">
|
||||
<h3><a class="toc-backref" href="#id30">How do I send mail from a Python script?</a><a class="headerlink" href="#how-do-i-send-mail-from-a-python-script" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Use the standard library module <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a>.</p>
|
||||
<p>Here’s a very simple interactive mail sender that uses it. This method will
|
||||
work on any host that supports an SMTP listener.</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">smtplib</span>
|
||||
|
||||
<span class="n">fromaddr</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">"From: "</span><span class="p">)</span>
|
||||
<span class="n">toaddrs</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">"To: "</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">','</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Enter message, end with ^D:"</span><span class="p">)</span>
|
||||
<span class="n">msg</span> <span class="o">=</span> <span class="s1">''</span>
|
||||
<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
|
||||
<span class="n">line</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
|
||||
<span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="p">:</span>
|
||||
<span class="k">break</span>
|
||||
<span class="n">msg</span> <span class="o">+=</span> <span class="n">line</span>
|
||||
|
||||
<span class="c1"># The actual mail send</span>
|
||||
<span class="n">server</span> <span class="o">=</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">)</span>
|
||||
<span class="n">server</span><span class="o">.</span><span class="n">sendmail</span><span class="p">(</span><span class="n">fromaddr</span><span class="p">,</span> <span class="n">toaddrs</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span>
|
||||
<span class="n">server</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>A Unix-only alternative uses sendmail. The location of the sendmail program
|
||||
varies between systems; sometimes it is <code class="docutils literal notranslate"><span class="pre">/usr/lib/sendmail</span></code>, sometimes
|
||||
<code class="docutils literal notranslate"><span class="pre">/usr/sbin/sendmail</span></code>. The sendmail manual page will help you out. Here’s
|
||||
some sample code:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
|
||||
|
||||
<span class="n">SENDMAIL</span> <span class="o">=</span> <span class="s2">"/usr/sbin/sendmail"</span> <span class="c1"># sendmail location</span>
|
||||
<span class="n">p</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="s2">"</span><span class="si">%s</span><span class="s2"> -t -i"</span> <span class="o">%</span> <span class="n">SENDMAIL</span><span class="p">,</span> <span class="s2">"w"</span><span class="p">)</span>
|
||||
<span class="n">p</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"To: receiver@example.com</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
|
||||
<span class="n">p</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"Subject: test</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
|
||||
<span class="n">p</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span> <span class="c1"># blank line separating headers from body</span>
|
||||
<span class="n">p</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"Some text</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
|
||||
<span class="n">p</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"some more text</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
|
||||
<span class="n">sts</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
||||
<span class="k">if</span> <span class="n">sts</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Sendmail exit status"</span><span class="p">,</span> <span class="n">sts</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-avoid-blocking-in-the-connect-method-of-a-socket">
|
||||
<h3><a class="toc-backref" href="#id31">How do I avoid blocking in the connect() method of a socket?</a><a class="headerlink" href="#how-do-i-avoid-blocking-in-the-connect-method-of-a-socket" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The <a class="reference internal" href="../library/select.html#module-select" title="select: Wait for I/O completion on multiple streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code></a> module is commonly used to help with asynchronous I/O on
|
||||
sockets.</p>
|
||||
<p>To prevent the TCP connect from blocking, you can set the socket to non-blocking
|
||||
mode. Then when you do the <code class="docutils literal notranslate"><span class="pre">connect()</span></code>, you will either connect immediately
|
||||
(unlikely) or get an exception that contains the error number as <code class="docutils literal notranslate"><span class="pre">.errno</span></code>.
|
||||
<code class="docutils literal notranslate"><span class="pre">errno.EINPROGRESS</span></code> indicates that the connection is in progress, but hasn’t
|
||||
finished yet. Different OSes will return different values, so you’re going to
|
||||
have to check what’s returned on your system.</p>
|
||||
<p>You can use the <code class="docutils literal notranslate"><span class="pre">connect_ex()</span></code> method to avoid creating an exception. It will
|
||||
just return the errno value. To poll, you can call <code class="docutils literal notranslate"><span class="pre">connect_ex()</span></code> again later
|
||||
– <code class="docutils literal notranslate"><span class="pre">0</span></code> or <code class="docutils literal notranslate"><span class="pre">errno.EISCONN</span></code> indicate that you’re connected – or you can pass this
|
||||
socket to select to check if it’s writable.</p>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<p>The <a class="reference internal" href="../library/asyncore.html#module-asyncore" title="asyncore: A base class for developing asynchronous socket handling services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncore</span></code></a> module presents a framework-like approach to the problem
|
||||
of writing non-blocking networking code.
|
||||
The third-party <a class="reference external" href="https://twistedmatrix.com/trac/">Twisted</a> library is
|
||||
a popular and feature-rich alternative.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="databases">
|
||||
<h2><a class="toc-backref" href="#id32">Databases</a><a class="headerlink" href="#databases" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="are-there-any-interfaces-to-database-packages-in-python">
|
||||
<h3><a class="toc-backref" href="#id33">Are there any interfaces to database packages in Python?</a><a class="headerlink" href="#are-there-any-interfaces-to-database-packages-in-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>Yes.</p>
|
||||
<p>Interfaces to disk-based hashes such as <a class="reference internal" href="../library/dbm.html#module-dbm.ndbm" title="dbm.ndbm: The standard "database" interface, based on ndbm. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">DBM</span></code></a> and <a class="reference internal" href="../library/dbm.html#module-dbm.gnu" title="dbm.gnu: GNU's reinterpretation of dbm. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">GDBM</span></code></a> are also included with standard Python. There is also the
|
||||
<a class="reference internal" href="../library/sqlite3.html#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code></a> module, which provides a lightweight disk-based relational
|
||||
database.</p>
|
||||
<p>Support for most relational databases is available. See the
|
||||
<a class="reference external" href="https://wiki.python.org/moin/DatabaseProgramming">DatabaseProgramming wiki page</a> for details.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-you-implement-persistent-objects-in-python">
|
||||
<h3><a class="toc-backref" href="#id34">How do you implement persistent objects in Python?</a><a class="headerlink" href="#how-do-you-implement-persistent-objects-in-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> library module solves this in a very general way (though you
|
||||
still can’t store things like open files, sockets or windows), and the
|
||||
<a class="reference internal" href="../library/shelve.html#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a> library module uses pickle and (g)dbm to create persistent
|
||||
mappings containing arbitrary Python objects.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="mathematics-and-numerics">
|
||||
<h2><a class="toc-backref" href="#id35">Mathematics and Numerics</a><a class="headerlink" href="#mathematics-and-numerics" title="Permalink to this headline">¶</a></h2>
|
||||
<div class="section" id="how-do-i-generate-random-numbers-in-python">
|
||||
<h3><a class="toc-backref" href="#id36">How do I generate random numbers in Python?</a><a class="headerlink" href="#how-do-i-generate-random-numbers-in-python" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The standard module <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">random</span></code></a> implements a random number generator. Usage
|
||||
is simple:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">random</span>
|
||||
<span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>This returns a random floating point number in the range [0, 1).</p>
|
||||
<p>There are also many other specialized generators in this module, such as:</p>
|
||||
<ul class="simple">
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">randrange(a,</span> <span class="pre">b)</span></code> chooses an integer in the range [a, b).</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">uniform(a,</span> <span class="pre">b)</span></code> chooses a floating point number in the range [a, b).</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">normalvariate(mean,</span> <span class="pre">sdev)</span></code> samples the normal (Gaussian) distribution.</p></li>
|
||||
</ul>
|
||||
<p>Some higher-level functions operate on sequences directly, such as:</p>
|
||||
<ul class="simple">
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">choice(S)</span></code> chooses random element from a given sequence</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">shuffle(L)</span></code> shuffles a list in-place, i.e. permutes it randomly</p></li>
|
||||
</ul>
|
||||
<p>There’s also a <code class="docutils literal notranslate"><span class="pre">Random</span></code> class you can instantiate to create independent
|
||||
multiple random number generators.</p>
|
||||
</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="#">Library and Extension FAQ</a><ul>
|
||||
<li><a class="reference internal" href="#general-library-questions">General Library Questions</a></li>
|
||||
<li><a class="reference internal" href="#common-tasks">Common tasks</a></li>
|
||||
<li><a class="reference internal" href="#threads">Threads</a></li>
|
||||
<li><a class="reference internal" href="#input-and-output">Input and Output</a></li>
|
||||
<li><a class="reference internal" href="#network-internet-programming">Network/Internet Programming</a></li>
|
||||
<li><a class="reference internal" href="#databases">Databases</a></li>
|
||||
<li><a class="reference internal" href="#mathematics-and-numerics">Mathematics and Numerics</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>Previous topic</h4>
|
||||
<p class="topless"><a href="design.html"
|
||||
title="previous chapter">Design and History FAQ</a></p>
|
||||
<h4>Next topic</h4>
|
||||
<p class="topless"><a href="extending.html"
|
||||
title="next chapter">Extending/Embedding 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/library.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="extending.html" title="Extending/Embedding FAQ"
|
||||
>next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="design.html" title="Design and History 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>
|
1967
python-3.7.4-docs-html/faq/programming.html
Normal file
1967
python-3.7.4-docs-html/faq/programming.html
Normal file
File diff suppressed because it is too large
Load Diff
434
python-3.7.4-docs-html/faq/windows.html
Normal file
434
python-3.7.4-docs-html/faq/windows.html
Normal file
@@ -0,0 +1,434 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Python on Windows 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="Graphic User Interface FAQ" href="gui.html" />
|
||||
<link rel="prev" title="Extending/Embedding FAQ" href="extending.html" />
|
||||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/faq/windows.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="gui.html" title="Graphic User Interface FAQ"
|
||||
accesskey="N">next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="extending.html" title="Extending/Embedding 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="python-on-windows-faq">
|
||||
<span id="windows-faq"></span><h1><a class="toc-backref" href="#id1">Python on Windows FAQ</a><a class="headerlink" href="#python-on-windows-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="#python-on-windows-faq" id="id1">Python on Windows FAQ</a></p>
|
||||
<ul>
|
||||
<li><p><a class="reference internal" href="#how-do-i-run-a-python-program-under-windows" id="id2">How do I run a Python program under Windows?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-make-python-scripts-executable" id="id3">How do I make Python scripts executable?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#why-does-python-sometimes-take-so-long-to-start" id="id4">Why does Python sometimes take so long to start?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-make-an-executable-from-a-python-script" id="id5">How do I make an executable from a Python script?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#is-a-pyd-file-the-same-as-a-dll" id="id6">Is a <code class="docutils literal notranslate"><span class="pre">*.pyd</span></code> file the same as a DLL?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-can-i-embed-python-into-a-windows-application" id="id7">How can I embed Python into a Windows application?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source" id="id8">How do I keep editors from inserting tabs into my Python source?</a></p></li>
|
||||
<li><p><a class="reference internal" href="#how-do-i-check-for-a-keypress-without-blocking" id="id9">How do I check for a keypress without blocking?</a></p></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-run-a-python-program-under-windows">
|
||||
<span id="faq-run-program-under-windows"></span><h2><a class="toc-backref" href="#id2">How do I run a Python program under Windows?</a><a class="headerlink" href="#how-do-i-run-a-python-program-under-windows" title="Permalink to this headline">¶</a></h2>
|
||||
<p>This is not necessarily a straightforward question. If you are already familiar
|
||||
with running programs from the Windows command line then everything will seem
|
||||
obvious; otherwise, you might need a little more guidance.</p>
|
||||
<p>Unless you use some sort of integrated development environment, you will end up
|
||||
<em>typing</em> Windows commands into what is variously referred to as a “DOS window”
|
||||
or “Command prompt window”. Usually you can create such a window from your
|
||||
search bar by searching for <code class="docutils literal notranslate"><span class="pre">cmd</span></code>. You should be able to recognize
|
||||
when you have started such a window because you will see a Windows “command
|
||||
prompt”, which usually looks like this:</p>
|
||||
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">C:\></span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The letter may be different, and there might be other things after it, so you
|
||||
might just as easily see something like:</p>
|
||||
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">D:\YourName\Projects\Python></span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>depending on how your computer has been set up and what else you have recently
|
||||
done with it. Once you have started such a window, you are well on the way to
|
||||
running Python programs.</p>
|
||||
<p>You need to realize that your Python scripts have to be processed by another
|
||||
program called the Python <em>interpreter</em>. The interpreter reads your script,
|
||||
compiles it into bytecodes, and then executes the bytecodes to run your
|
||||
program. So, how do you arrange for the interpreter to handle your Python?</p>
|
||||
<p>First, you need to make sure that your command window recognises the word
|
||||
“py” as an instruction to start the interpreter. If you have opened a
|
||||
command window, you should try entering the command <code class="docutils literal notranslate"><span class="pre">py</span></code> and hitting
|
||||
return:</p>
|
||||
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">C:\Users\YourName></span> py
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>You should then see something like:</p>
|
||||
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="go">Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32</span>
|
||||
<span class="go">Type "help", "copyright", "credits" or "license" for more information.</span>
|
||||
<span class="go">>>></span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>You have started the interpreter in “interactive mode”. That means you can enter
|
||||
Python statements or expressions interactively and have them executed or
|
||||
evaluated while you wait. This is one of Python’s strongest features. Check it
|
||||
by entering a few expressions of your choice and seeing the results:</p>
|
||||
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="s2">"Hello"</span><span class="p">)</span>
|
||||
<span class="go">Hello</span>
|
||||
<span class="gp">>>> </span><span class="s2">"Hello"</span> <span class="o">*</span> <span class="mi">3</span>
|
||||
<span class="go">'HelloHelloHello'</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Many people use the interactive mode as a convenient yet highly programmable
|
||||
calculator. When you want to end your interactive Python session,
|
||||
call the <a class="reference internal" href="../library/constants.html#exit" title="exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">exit()</span></code></a> function or hold the <kbd class="kbd docutils literal notranslate">Ctrl</kbd> key down
|
||||
while you enter a <kbd class="kbd docutils literal notranslate">Z</kbd>, then hit the “<kbd class="kbd docutils literal notranslate">Enter</kbd>” key to get
|
||||
back to your Windows command prompt.</p>
|
||||
<p>You may also find that you have a Start-menu entry such as <span class="menuselection">Start
|
||||
‣ Programs ‣ Python 3.x ‣ Python (command line)</span> that results in you
|
||||
seeing the <code class="docutils literal notranslate"><span class="pre">>>></span></code> prompt in a new window. If so, the window will disappear
|
||||
after you call the <a class="reference internal" href="../library/constants.html#exit" title="exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">exit()</span></code></a> function or enter the <kbd class="kbd docutils literal notranslate">Ctrl-Z</kbd>
|
||||
character; Windows is running a single “python”
|
||||
command in the window, and closes it when you terminate the interpreter.</p>
|
||||
<p>Now that we know the <code class="docutils literal notranslate"><span class="pre">py</span></code> command is recognized, you can give your
|
||||
Python script to it. You’ll have to give either an absolute or a
|
||||
relative path to the Python script. Let’s say your Python script is
|
||||
located in your desktop and is named <code class="docutils literal notranslate"><span class="pre">hello.py</span></code>, and your command
|
||||
prompt is nicely opened in your home directory so you’re seeing something
|
||||
similar to:</p>
|
||||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>C:\Users\YourName>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>So now you’ll ask the <code class="docutils literal notranslate"><span class="pre">py</span></code> command to give your script to Python by
|
||||
typing <code class="docutils literal notranslate"><span class="pre">py</span></code> followed by your script path:</p>
|
||||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>C:\Users\YourName> py Desktop\hello.py
|
||||
hello
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-make-python-scripts-executable">
|
||||
<h2><a class="toc-backref" href="#id3">How do I make Python scripts executable?</a><a class="headerlink" href="#how-do-i-make-python-scripts-executable" title="Permalink to this headline">¶</a></h2>
|
||||
<p>On Windows, the standard Python installer already associates the .py
|
||||
extension with a file type (Python.File) and gives that file type an open
|
||||
command that runs the interpreter (<code class="docutils literal notranslate"><span class="pre">D:\Program</span> <span class="pre">Files\Python\python.exe</span> <span class="pre">"%1"</span>
|
||||
<span class="pre">%*</span></code>). This is enough to make scripts executable from the command prompt as
|
||||
‘foo.py’. If you’d rather be able to execute the script by simple typing ‘foo’
|
||||
with no extension you need to add .py to the PATHEXT environment variable.</p>
|
||||
</div>
|
||||
<div class="section" id="why-does-python-sometimes-take-so-long-to-start">
|
||||
<h2><a class="toc-backref" href="#id4">Why does Python sometimes take so long to start?</a><a class="headerlink" href="#why-does-python-sometimes-take-so-long-to-start" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Usually Python starts very quickly on Windows, but occasionally there are bug
|
||||
reports that Python suddenly begins to take a long time to start up. This is
|
||||
made even more puzzling because Python will work fine on other Windows systems
|
||||
which appear to be configured identically.</p>
|
||||
<p>The problem may be caused by a misconfiguration of virus checking software on
|
||||
the problem machine. Some virus scanners have been known to introduce startup
|
||||
overhead of two orders of magnitude when the scanner is configured to monitor
|
||||
all reads from the filesystem. Try checking the configuration of virus scanning
|
||||
software on your systems to ensure that they are indeed configured identically.
|
||||
McAfee, when configured to scan all file system read activity, is a particular
|
||||
offender.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-make-an-executable-from-a-python-script">
|
||||
<h2><a class="toc-backref" href="#id5">How do I make an executable from a Python script?</a><a class="headerlink" href="#how-do-i-make-an-executable-from-a-python-script" title="Permalink to this headline">¶</a></h2>
|
||||
<p>See <a class="reference external" href="https://anthony-tuininga.github.io/cx_Freeze/">cx_Freeze</a> for a distutils extension
|
||||
that allows you to create console and GUI executables from Python code.
|
||||
<a class="reference external" href="http://www.py2exe.org/">py2exe</a>, the most popular extension for building
|
||||
Python 2.x-based executables, does not yet support Python 3 but a version that
|
||||
does is in development.</p>
|
||||
</div>
|
||||
<div class="section" id="is-a-pyd-file-the-same-as-a-dll">
|
||||
<h2><a class="toc-backref" href="#id6">Is a <code class="docutils literal notranslate"><span class="pre">*.pyd</span></code> file the same as a DLL?</a><a class="headerlink" href="#is-a-pyd-file-the-same-as-a-dll" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Yes, .pyd files are dll’s, but there are a few differences. If you have a DLL
|
||||
named <code class="docutils literal notranslate"><span class="pre">foo.pyd</span></code>, then it must have a function <code class="docutils literal notranslate"><span class="pre">PyInit_foo()</span></code>. You can then
|
||||
write Python “import foo”, and Python will search for foo.pyd (as well as
|
||||
foo.py, foo.pyc) and if it finds it, will attempt to call <code class="docutils literal notranslate"><span class="pre">PyInit_foo()</span></code> to
|
||||
initialize it. You do not link your .exe with foo.lib, as that would cause
|
||||
Windows to require the DLL to be present.</p>
|
||||
<p>Note that the search path for foo.pyd is PYTHONPATH, not the same as the path
|
||||
that Windows uses to search for foo.dll. Also, foo.pyd need not be present to
|
||||
run your program, whereas if you linked your program with a dll, the dll is
|
||||
required. Of course, foo.pyd is required if you want to say <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">foo</span></code>. In
|
||||
a DLL, linkage is declared in the source code with <code class="docutils literal notranslate"><span class="pre">__declspec(dllexport)</span></code>.
|
||||
In a .pyd, linkage is defined in a list of available functions.</p>
|
||||
</div>
|
||||
<div class="section" id="how-can-i-embed-python-into-a-windows-application">
|
||||
<h2><a class="toc-backref" href="#id7">How can I embed Python into a Windows application?</a><a class="headerlink" href="#how-can-i-embed-python-into-a-windows-application" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Embedding the Python interpreter in a Windows app can be summarized as follows:</p>
|
||||
<ol class="arabic">
|
||||
<li><p>Do _not_ build Python into your .exe file directly. On Windows, Python must
|
||||
be a DLL to handle importing modules that are themselves DLL’s. (This is the
|
||||
first key undocumented fact.) Instead, link to <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code>; it is
|
||||
typically installed in <code class="docutils literal notranslate"><span class="pre">C:\Windows\System</span></code>. <em>NN</em> is the Python version, a
|
||||
number such as “33” for Python 3.3.</p>
|
||||
<p>You can link to Python in two different ways. Load-time linking means
|
||||
linking against <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.lib</span></code>, while run-time linking means linking
|
||||
against <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code>. (General note: <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.lib</span></code> is the
|
||||
so-called “import lib” corresponding to <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code>. It merely
|
||||
defines symbols for the linker.)</p>
|
||||
<p>Run-time linking greatly simplifies link options; everything happens at run
|
||||
time. Your code must load <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code> using the Windows
|
||||
<code class="docutils literal notranslate"><span class="pre">LoadLibraryEx()</span></code> routine. The code must also use access routines and data
|
||||
in <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.dll</span></code> (that is, Python’s C API’s) using pointers obtained
|
||||
by the Windows <code class="docutils literal notranslate"><span class="pre">GetProcAddress()</span></code> routine. Macros can make using these
|
||||
pointers transparent to any C code that calls routines in Python’s C API.</p>
|
||||
<p>Borland note: convert <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">NN</span></em><span class="pre">.lib</span></code> to OMF format using Coff2Omf.exe
|
||||
first.</p>
|
||||
</li>
|
||||
<li><p>If you use SWIG, it is easy to create a Python “extension module” that will
|
||||
make the app’s data and methods available to Python. SWIG will handle just
|
||||
about all the grungy details for you. The result is C code that you link
|
||||
<em>into</em> your .exe file (!) You do _not_ have to create a DLL file, and this
|
||||
also simplifies linking.</p></li>
|
||||
<li><p>SWIG will create an init function (a C function) whose name depends on the
|
||||
name of the extension module. For example, if the name of the module is leo,
|
||||
the init function will be called initleo(). If you use SWIG shadow classes,
|
||||
as you should, the init function will be called initleoc(). This initializes
|
||||
a mostly hidden helper class used by the shadow class.</p>
|
||||
<p>The reason you can link the C code in step 2 into your .exe file is that
|
||||
calling the initialization function is equivalent to importing the module
|
||||
into Python! (This is the second key undocumented fact.)</p>
|
||||
</li>
|
||||
<li><p>In short, you can use the following code to initialize the Python interpreter
|
||||
with your extension module.</p>
|
||||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">"python.h"</span><span class="cp"></span>
|
||||
<span class="p">...</span>
|
||||
<span class="n">Py_Initialize</span><span class="p">();</span> <span class="c1">// Initialize Python.</span>
|
||||
<span class="n">initmyAppc</span><span class="p">();</span> <span class="c1">// Initialize (import) the helper class.</span>
|
||||
<span class="n">PyRun_SimpleString</span><span class="p">(</span><span class="s">"import myApp"</span><span class="p">);</span> <span class="c1">// Import the shadow class.</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</li>
|
||||
<li><p>There are two problems with Python’s C API which will become apparent if you
|
||||
use a compiler other than MSVC, the compiler used to build pythonNN.dll.</p>
|
||||
<p>Problem 1: The so-called “Very High Level” functions that take FILE *
|
||||
arguments will not work in a multi-compiler environment because each
|
||||
compiler’s notion of a struct FILE will be different. From an implementation
|
||||
standpoint these are very _low_ level functions.</p>
|
||||
<p>Problem 2: SWIG generates the following code when generating wrappers to void
|
||||
functions:</p>
|
||||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_INCREF</span><span class="p">(</span><span class="n">Py_None</span><span class="p">);</span>
|
||||
<span class="n">_resultobj</span> <span class="o">=</span> <span class="n">Py_None</span><span class="p">;</span>
|
||||
<span class="k">return</span> <span class="n">_resultobj</span><span class="p">;</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Alas, Py_None is a macro that expands to a reference to a complex data
|
||||
structure called _Py_NoneStruct inside pythonNN.dll. Again, this code will
|
||||
fail in a mult-compiler environment. Replace such code by:</p>
|
||||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nf">Py_BuildValue</span><span class="p">(</span><span class="s">""</span><span class="p">);</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>It may be possible to use SWIG’s <code class="docutils literal notranslate"><span class="pre">%typemap</span></code> command to make the change
|
||||
automatically, though I have not been able to get this to work (I’m a
|
||||
complete SWIG newbie).</p>
|
||||
</li>
|
||||
<li><p>Using a Python shell script to put up a Python interpreter window from inside
|
||||
your Windows app is not a good idea; the resulting window will be independent
|
||||
of your app’s windowing system. Rather, you (or the wxPythonWindow class)
|
||||
should create a “native” interpreter window. It is easy to connect that
|
||||
window to the Python interpreter. You can redirect Python’s i/o to _any_
|
||||
object that supports read and write, so all you need is a Python object
|
||||
(defined in your extension module) that contains read() and write() methods.</p></li>
|
||||
</ol>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-keep-editors-from-inserting-tabs-into-my-python-source">
|
||||
<h2><a class="toc-backref" href="#id8">How do I keep editors from inserting tabs into my Python source?</a><a class="headerlink" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source" title="Permalink to this headline">¶</a></h2>
|
||||
<p>The FAQ does not recommend using tabs, and the Python style guide, <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>,
|
||||
recommends 4 spaces for distributed Python code; this is also the Emacs
|
||||
python-mode default.</p>
|
||||
<p>Under any editor, mixing tabs and spaces is a bad idea. MSVC is no different in
|
||||
this respect, and is easily configured to use spaces: Take <span class="menuselection">Tools
|
||||
‣ Options ‣ Tabs</span>, and for file type “Default” set “Tab size” and “Indent
|
||||
size” to 4, and select the “Insert spaces” radio button.</p>
|
||||
<p>Python raises <a class="reference internal" href="../library/exceptions.html#IndentationError" title="IndentationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndentationError</span></code></a> or <a class="reference internal" href="../library/exceptions.html#TabError" title="TabError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TabError</span></code></a> if mixed tabs
|
||||
and spaces are causing problems in leading whitespace.
|
||||
You may also run the <a class="reference internal" href="../library/tabnanny.html#module-tabnanny" title="tabnanny: Tool for detecting white space related problems in Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tabnanny</span></code></a> module to check a directory tree
|
||||
in batch mode.</p>
|
||||
</div>
|
||||
<div class="section" id="how-do-i-check-for-a-keypress-without-blocking">
|
||||
<h2><a class="toc-backref" href="#id9">How do I check for a keypress without blocking?</a><a class="headerlink" href="#how-do-i-check-for-a-keypress-without-blocking" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Use the msvcrt module. This is a standard Windows-specific extension module.
|
||||
It defines a function <code class="docutils literal notranslate"><span class="pre">kbhit()</span></code> which checks whether a keyboard hit is
|
||||
present, and <code class="docutils literal notranslate"><span class="pre">getch()</span></code> which gets one character without echoing it.</p>
|
||||
</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="#">Python on Windows FAQ</a><ul>
|
||||
<li><a class="reference internal" href="#how-do-i-run-a-python-program-under-windows">How do I run a Python program under Windows?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-make-python-scripts-executable">How do I make Python scripts executable?</a></li>
|
||||
<li><a class="reference internal" href="#why-does-python-sometimes-take-so-long-to-start">Why does Python sometimes take so long to start?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-make-an-executable-from-a-python-script">How do I make an executable from a Python script?</a></li>
|
||||
<li><a class="reference internal" href="#is-a-pyd-file-the-same-as-a-dll">Is a <code class="docutils literal notranslate"><span class="pre">*.pyd</span></code> file the same as a DLL?</a></li>
|
||||
<li><a class="reference internal" href="#how-can-i-embed-python-into-a-windows-application">How can I embed Python into a Windows application?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-keep-editors-from-inserting-tabs-into-my-python-source">How do I keep editors from inserting tabs into my Python source?</a></li>
|
||||
<li><a class="reference internal" href="#how-do-i-check-for-a-keypress-without-blocking">How do I check for a keypress without blocking?</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>Previous topic</h4>
|
||||
<p class="topless"><a href="extending.html"
|
||||
title="previous chapter">Extending/Embedding FAQ</a></p>
|
||||
<h4>Next topic</h4>
|
||||
<p class="topless"><a href="gui.html"
|
||||
title="next chapter">Graphic User Interface 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/windows.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="gui.html" title="Graphic User Interface FAQ"
|
||||
>next</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="extending.html" title="Extending/Embedding 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>
|
Reference in New Issue
Block a user