formations/python/python3/en/_build/html/introspection.html

297 lines
19 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Instrospection &#8212; pyfundoc documentation</title>
<link rel="stylesheet" href="_static/bizstyle.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: ' ',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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/bizstyle.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Coding standards" href="codingstandards.html" />
<link rel="prev" title="Lets play with builtin types" href="base-type2.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!--[if lt IE 9]>
<script type="text/javascript" src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</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="codingstandards.html" title="Coding standards"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="base-type2.html" title="Lets play with builtin types"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">pyfundoc documentation</a> &#187;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body" role="main">
<div class="section" id="instrospection">
<h1>Instrospection<a class="headerlink" href="#instrospection" title="Permalink to this headline"></a></h1>
<p>Pythons introspection capabilites. First, we shall use the prompt.</p>
<div class="section" id="the-prompt">
<h2>The prompt<a class="headerlink" href="#the-prompt" title="Permalink to this headline"></a></h2>
<p>Type <cite>python3</cite> in the prompt. It gives you a prompt, which is the python prompt:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>The prompt enables us to deeply analyse the fundamentals of the language.
It is interesting for a good understanding of the concepts.</p>
<p>For example, <strong>introspection</strong>. Introspection means finding informations
about each element you want to manipulate:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">locals</span><span class="p">()</span>
<span class="go">{&#39;__name__&#39;: &#39;__main__&#39;, &#39;__package__&#39;: None, &#39;__doc__&#39;: None,</span>
<span class="go">&#39;__builtins__&#39;: &lt;module &#39;builtins&#39; (built-in)&gt;, (...) }</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<dl class="glossary docutils">
<dt id="term-locals">locals</dt>
<dd>The <cite>locals()</cite> builtin function lists the variables that lives in the
current namespace.</dd>
<dt id="term-globals">globals</dt>
<dd>The <cite>globals()</cite> builtin function lists the variables that lives in the
general namespace.</dd>
</dl>
<p><cite>locals()</cite> in the global namespace and <cite>locals()`</cite> in a local namespace
(a functions namespace):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">myfunc</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">a</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="nb">locals</span><span class="p">())</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">locals</span><span class="p">()</span>
<span class="go">{&#39;__package__&#39;: None, &#39;__spec__&#39;: None, (...)&#39;__name__&#39;: &#39;__main__&#39;, &#39;__builtins__&#39;: &lt;module &#39;builtins&#39; (built-in)&gt;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># we can see that there&#39;s no &quot;a&quot; variable</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">myfunc</span><span class="p">(</span><span class="s2">&quot;toto&quot;</span><span class="p">)</span>
<span class="go">{&#39;x&#39;: &#39;toto&#39;, &#39;a&#39;: 2}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># in the function there is a &quot;a&quot; variable</span>
</pre></div>
</div>
<p>When we open a prompt, the general namespace and the local namespace are the same.</p>
<dl class="glossary docutils">
<dt id="term-builtins">builtins</dt>
<dd>The <cite>__builtins__`</cite> module is a special module :
it is the only namespace which is automaticaly imported.
It means that the variables function that are in the builtins are always
accessible. You can always call them.</dd>
</dl>
<p>For example, you can use the function <cite>dir</cite> which is directly accessible
without importing any module:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">()</span>
<span class="go">[&#39;__builtins__&#39;, &#39;__cached__&#39;, &#39;__doc__&#39;, &#39;__loader__&#39;, &#39;__name__&#39;, ...]</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>In fact, the <cite>dir()</cite> function lives in the <cite>__builtins__</cite>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">__builtins__</span><span class="o">.</span><span class="n">dir</span><span class="p">()</span>
<span class="go">[&#39;__builtins__&#39;, &#39;__cached__&#39;, &#39;__doc__&#39;, &#39;__loader__&#39;, &#39;__name__&#39;, ...]</span>
</pre></div>
</div>
<p>type:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">help</span><span class="p">(</span><span class="nb">dir</span><span class="p">)</span>
</pre></div>
</div>
<p>to have help on a given function or module.</p>
<p><cite>__doc__</cite>: this namespaces docstring
<cite>__package__</cite>: the current namespace</p>
<p><cite>__name__</cite> is the name of the current namespace, which is <cite>__main__</cite>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="vm">__name__</span>
<span class="go">&#39;__main__&#39;</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>why is it <cite>__main__</cite> ? Because we are located in the global namespace.</p>
</div>
<div class="section" id="modules">
<h2>Modules<a class="headerlink" href="#modules" title="Permalink to this headline"></a></h2>
<p>A module is a namespace, it is a pyhton file that you can import in another module.
A python file is intended to become a library, but you can use it as a script.</p>
<p>If you want to use module as a script, the good pratice is to fix its behavior with:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
<span class="n">do_this</span><span class="p">()</span>
</pre></div>
</div>
<p>It means that if you launch the module in the global namespace, then the module
is not used as a library but as a standalone script.
The good practice in a module is to do nothing.
If you launch:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">do_this</span><span class="p">()</span>
</pre></div>
</div>
<p>without the test condition:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
</pre></div>
</div>
<p>when you import the module:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mymodule</span>
</pre></div>
</div>
<p>it will launch the <cite>do_this</cite> function, wich is not a intended behavior for a library.
A library is just a set of function/classes that are available in a given namespace.
A script is intended to do something when launched, not a library.</p>
<p>What are scripts ?
Scripts are executable file, they have a she bang:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python3</span>
</pre></div>
</div>
<p>You can import some other libraries in your script, and it becomes an application:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">my_second_module</span>
</pre></div>
</div>
<p>or:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">my_second_module</span> <span class="k">import</span> <span class="n">my_function</span>
</pre></div>
</div>
<p>or:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">my_second_module</span> <span class="k">import</span> <span class="n">my_function</span> <span class="k">as</span> <span class="n">my_function_alias</span>
</pre></div>
</div>
<p>or:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">my_second_module</span> <span class="k">as</span> <span class="nn">my_second_module_alias</span>
</pre></div>
</div>
</div>
<div class="section" id="assinging-a-variable">
<h2>Assinging a variable<a class="headerlink" href="#assinging-a-variable" title="Permalink to this headline"></a></h2>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="mf">42.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">&lt;class &#39;float&#39;&gt;</span>
</pre></div>
</div>
<p>what is dynamic typing ? You can set some other value to the <cite>a</cite> variable that
has nothing to do with the previous type:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="mi">42</span><span class="n">j</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">&lt;class &#39;complex&#39;&gt;</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="functions">
<h2>Functions<a class="headerlink" href="#functions" title="Permalink to this headline"></a></h2>
<p>This function:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">my_function</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="gp">... </span> <span class="sd">&quot;&quot;&quot;this is my func docstring&quot;&quot;&quot;</span>
<span class="gp">...</span>
</pre></div>
</div>
<p>is equivalent to:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">my_function</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="gp">... </span> <span class="sd">&quot;&quot;&quot;this is my func docstring&quot;&quot;&quot;</span>
<span class="gp">... </span> <span class="k">return</span> <span class="kc">None</span>
</pre></div>
</div>
<p>There are no procedures in python (a procedure returns nothing). It means that
a function return always someting. Even if you dont <em>return</em> something: it
returns <cite>None</cite>, which is something:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">result</span> <span class="o">=</span> <span class="n">my_function</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">my_function</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="go">None</span>
</pre></div>
</div>
<p><strong>In python, a function is… an object</strong>. Yes. What is a docstrings function
? A Docstring is the <cite>__doc__</cite> attribute of the <cite>my_function</cite> object:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">my_function</span><span class="o">.</span><span class="vm">__doc__</span>
<span class="go">&#39;this is my func docstring&#39;</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>In python, absolutely everything is an object… even the functions:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span> <span class="p">(</span><span class="n">my_function</span><span class="p">)</span>
<span class="go">&lt;class &#39;function&#39;&gt;</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>A function is an instance of the <cite>function</cite> class.</p>
<p>Unnamed arguments in a function:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">my_fonction</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;arguments : &quot;</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">args</span><span class="p">))</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;named argumments : &quot;</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">kwargs</span><span class="p">))</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">my_fonction</span><span class="p">(</span><span class="s2">&quot;toto&quot;</span><span class="p">,</span> <span class="s2">&quot;titi&quot;</span><span class="p">,</span> <span class="n">tutu</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">tata</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="go">arguments : (&#39;toto&#39;, &#39;titi&#39;)</span>
<span class="go">named argumments : {&#39;tata&#39;: 3, &#39;tutu&#39;: 2}</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<div class="section" id="the-samourai-rule">
<h3>The <em>samouraï</em> rule<a class="headerlink" href="#the-samourai-rule" title="Permalink to this headline"></a></h3>
<p>A function shall return the expected result, or… kill itself
(that is, raise and exception).</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">function_raise</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">==</span> <span class="nb">int</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Not the right type&quot;</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">else</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">e</span> <span class="o">=</span> <span class="n">function_raise</span><span class="p">(</span><span class="s2">&quot;une string&quot;</span><span class="p">)</span>
<span class="gp">... </span><span class="k">except</span> <span class="ne">TypeError</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="n">e</span>
<span class="gp">...</span>
<span class="go">Not the right type</span>
<span class="go">&gt;&gt;</span>
</pre></div>
</div>
</div>
</div>
</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="codingstandards.html" title="Coding standards"
>next</a> |</li>
<li class="right" >
<a href="base-type2.html" title="Lets play with builtin types"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">pyfundoc documentation</a> &#187;</li>
</ul>
</div>
<div class="footer" role="contentinfo">
</div>
</body>
</html>