formations/python/python3/fr/_build/html/classes.html
2018-08-21 10:25:44 +02:00

676 lines
56 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 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Définir et manipuler des classes &#8212; Documentation Formation Python 1</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1',
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/translations.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Recherche" href="search.html" />
<link rel="next" title="Interactions avec lutilisateur" href="prompt.html" />
<link rel="prev" title="Sphinx et docutils" href="docutils.html" />
</head>
<body>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<table><tr><td>
<img src="_static/sphinx.png" alt="logo" />
</td><td>
<h1>&nbsp; &nbsp; Programmation python</h1>
</td></tr></table>
</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="Index général"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Index des modules Python"
>modules</a> |</li>
<li class="right" >
<a href="prompt.html" title="Interactions avec lutilisateur"
accesskey="N">suivant</a> |</li>
<li class="right" >
<a href="docutils.html" title="Sphinx et docutils"
accesskey="P">précédent</a> |</li>
<a href="index.html">Programmation python </a> &raquo;
</ul>
</div>
<p>&nbsp;</p>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="definir-et-manipuler-des-classes">
<h1>Définir et manipuler des classes<a class="headerlink" href="#definir-et-manipuler-des-classes" title="Lien permanent vers ce titre"></a></h1>
<dl class="glossary docutils">
<dt id="term-objet">objet</dt>
<dd>Un object est une entité possédant un type, un état, et un comportement.
Un object correspondant généralement à une entité du monde réel, mais
cette entité peut être abstraite.
On parle aussi d”<strong>instance</strong>.</dd>
</dl>
<p><strong>état dun objet</strong> : ensemble de propriétés auxquelles sont associées des
valeurs.</p>
<p>Les variables de lobjet sont appelées des <strong>attributs</strong>.
le comportement dun <a class="reference internal" href="#term-objet"><span class="xref std std-term">objet</span></a> :</p>
<ul class="simple">
<li>des actions effectuées sur lobjet</li>
<li>des appels faits sur lobjet</li>
</ul>
<p>envois de messages à lobjet = appel de <strong>méthodes</strong></p>
<div class="section" id="programmation-objet-premiere-approche">
<h2>programmation objet (première approche)<a class="headerlink" href="#programmation-objet-premiere-approche" title="Lien permanent vers ce titre"></a></h2>
<ul class="simple">
<li>le type et le protocole dun objet sont définis par sa classe</li>
<li>une classe possède un ensemble dattributs et de méthodes</li>
</ul>
<div class="section" id="deux-relations-possibles">
<h3>deux relations possibles<a class="headerlink" href="#deux-relations-possibles" title="Lien permanent vers ce titre"></a></h3>
<dl class="glossary docutils">
<dt id="term-heritage">heritage</dt>
<dd>relation «&nbsp;est une espèce de &nbsp;» utilisée entre une classe et une autre classe</dd>
<dt id="term-instantiation">instantiation</dt>
<dd>relation «&nbsp;est une instance de &nbsp;» entre un objet et une classe</dd>
</dl>
<ul class="simple">
<li>est une instance de (objets par rapport à classe)</li>
<li>est une espèce de (classe par rapport à classe, <a class="reference internal" href="#term-heritage"><span class="xref std std-term">heritage</span></a>)</li>
</ul>
<p><strong>instance</strong></p>
<ul class="simple">
<li>définition dune classe</li>
<li>instance de classe : on peut créer des objets à partir dun type «&nbsp;classe&nbsp;»
(une classe est instanciable)</li>
</ul>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
</pre></div>
</div>
<p><a class="reference internal" href="#term-heritage"><span class="xref std std-term">heritage</span></a> : notation en python</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">B</span><span class="p">(</span><span class="n">A</span><span class="p">):</span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">B</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="o">==</span> <span class="n">B</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">A</span><span class="p">)</span> <span class="o">==</span> <span class="kc">True</span>
</pre></div>
</div>
<p>possibilité en python dhéritage multiple:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="n">B</span><span class="p">,</span> <span class="n">C</span><span class="p">):</span> <span class="k">pass</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="attribut-d-objets-et-de-classes">
<h2>attribut dobjets et de classes<a class="headerlink" href="#attribut-d-objets-et-de-classes" title="Lien permanent vers ce titre"></a></h2>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">o</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">o</span>
<span class="go">&lt;object object at 0x7f77c9cda0d0&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">a</span>
<span class="go">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">vars</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="go">{&#39;a&#39;: 3}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{&#39;a&#39;: 3}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{&#39;__module__&#39;: &#39;__main__&#39;, &#39;__doc__&#39;: None}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="n">c</span> <span class="o">=</span> <span class="mi">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{&#39;c&#39;: 5, &#39;__module__&#39;: &#39;__main__&#39;, &#39;__doc__&#39;: None}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">c</span>
<span class="go">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">z</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">z</span>
<span class="go">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{&#39;a&#39;: 3, &#39;z&#39;: 3}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{&#39;c&#39;: 5, &#39;__module__&#39;: &#39;__main__&#39;, &#39;__doc__&#39;: None}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">MaKlass</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">unefonction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="n">x</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">MaKlass</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{&#39;__module__&#39;: &#39;__main__&#39;, &#39;__doc__&#39;: None, &#39;unefonction&#39;: &lt;function unefonction at 0x7f77c5b0c488&gt;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">k</span> <span class="o">=</span> <span class="n">MaKlass</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">k</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">autrefonc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>
<span class="k">def</span> <span class="nf">autrefonc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="o">^</span>
<span class="gr">SyntaxError</span>: <span class="n">invalid syntax</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">autrefonc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="n">x</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">k</span><span class="o">.</span><span class="n">autrefonc</span> <span class="o">=</span> <span class="n">autrefonc</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">k</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{&#39;autrefonc&#39;: &lt;function autrefonc at 0x7f77c5b0c500&gt;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">MaKlass</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{&#39;__module__&#39;: &#39;__main__&#39;, &#39;__doc__&#39;: None, &#39;unefonction&#39;: &lt;function unefonction at 0x7f77c5b0c488&gt;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">MaKlass</span><span class="o">.</span><span class="n">unefonction</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="s2">&quot;toto&quot;</span><span class="p">)</span>
<span class="go">toto</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">k</span><span class="o">.</span><span class="n">unefonction</span><span class="p">(</span><span class="s2">&quot;toto&quot;</span><span class="p">)</span>
<span class="go">toto</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">k</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{&#39;autrefonc&#39;: &lt;function autrefonc at 0x7f77c5b0c500&gt;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">MaKlass</span><span class="o">.</span><span class="n">toto</span> <span class="o">=</span> <span class="s2">&quot;test&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">k</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{&#39;autrefonc&#39;: &lt;function autrefonc at 0x7f77c5b0c500&gt;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">k</span><span class="o">.</span><span class="n">toto</span>
<span class="go">&#39;test&#39;</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="le-dict-avec-l-heritage-de-classe">
<h2>le __dict__ avec lhéritage de classe<a class="headerlink" href="#le-dict-avec-l-heritage-de-classe" title="Lien permanent vers ce titre"></a></h2>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">dict_proxy({&#39;__dict__&#39;: &lt;attribute &#39;__dict__&#39; of &#39;A&#39; objects&gt;, &#39;__module__&#39;: &#39;__main__&#39;, &#39;__weakref__&#39;: &lt;attribute &#39;__weakref__&#39; of &#39;A&#39; objects&gt;, &#39;__doc__&#39;: None})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">B</span><span class="p">(</span><span class="n">A</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">b</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="n">B</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">c</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">o</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">o</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">{}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">o</span><span class="o">.</span><span class="n">c</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">o</span><span class="o">.</span><span class="n">b</span>
<span class="go">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">o</span><span class="o">.</span><span class="vm">__class__</span>
<span class="go">&lt;class &#39;__main__.C&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">o</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__dict__</span>
<span class="go">dict_proxy({&#39;__module__&#39;: &#39;__main__&#39;, &#39;c&#39;: 2, &#39;__doc__&#39;: None})</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="method-resolution-object">
<h2>method resolution object<a class="headerlink" href="#method-resolution-object" title="Lien permanent vers ce titre"></a></h2>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="vm">__mro__</span>
<span class="go">(&lt;class &#39;__main__.A&#39;&gt;, &lt;type &#39;object&#39;&gt;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">B</span><span class="p">(</span><span class="n">A</span><span class="p">):</span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">B</span><span class="o">.</span><span class="vm">__mro__</span>
<span class="go">(&lt;class &#39;__main__.B&#39;&gt;, &lt;class &#39;__main__.A&#39;&gt;, &lt;type &#39;object&#39;&gt;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">):</span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">TypeError</span>: <span class="n">Error when calling the metaclass bases</span>
<span class="go"> Cannot create a consistent method resolution</span>
<span class="go">order (MRO) for bases A, B</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<div class="section" id="introspection-contre-encapsulation">
<h3>introspection contre encapsulation<a class="headerlink" href="#introspection-contre-encapsulation" title="Lien permanent vers ce titre"></a></h3>
<p>voir un objet comme un espace de nommage. Cest plus <strong>agile</strong>.</p>
<p>attributs et méthodes vus comme des ajouts dans lespace de nommage</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="n">a</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="n">x</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">f</span> <span class="o">=</span> <span class="n">function</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">f</span><span class="p">(</span><span class="s2">&quot;hello&quot;</span><span class="p">)</span>
<span class="go">hello</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="la-necessite-d-un-design-objet">
<h3>la nécessité dun design objet<a class="headerlink" href="#la-necessite-d-un-design-objet" title="Lien permanent vers ce titre"></a></h3>
<p>affichage dune calculette, il faut créer un type <cite>Touche</cite>
qui contient deux désignations : <cite>Chiffre</cite> et <cite>operation</cite>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nb">type</span> <span class="n">operation</span> <span class="o">=</span> <span class="n">Plus</span> <span class="o">|</span> <span class="n">Moins</span> <span class="o">|</span> <span class="n">Divise</span>
<span class="nb">type</span> <span class="n">touche</span> <span class="o">=</span> <span class="n">Chiffre</span> <span class="n">of</span> <span class="nb">int</span> <span class="o">|</span> <span class="n">Memoire</span> <span class="o">|</span> <span class="n">Op</span> <span class="n">of</span> <span class="n">operation</span>
</pre></div>
</div>
<p>soit, on définit un type touche, soit on ne définit pas ce type:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nb">type</span> <span class="n">operation</span> <span class="o">=</span> <span class="n">Plus</span> <span class="o">|</span> <span class="n">Moins</span> <span class="o">|</span> <span class="n">Divise</span>
<span class="nb">type</span> <span class="n">memoire</span> <span class="o">=</span> <span class="n">Memoire</span>
<span class="nb">type</span> <span class="n">chiffre</span> <span class="o">=</span> <span class="n">Chiffre</span>
</pre></div>
</div>
<ul class="simple">
<li>les structures de données (int, str, list, dict…) : types de base</li>
<li>les structures de données utilisateur : les classes !</li>
</ul>
<dl class="function">
<dt id="type">
<code class="descname">type</code><span class="sig-paren">(</span><em>objname</em><span class="sig-paren">)</span><a class="headerlink" href="#type" title="Lien permanent vers cette définition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Paramètres:</th><td class="field-body"><strong>objname</strong> lobjet dont on veut connaître le type</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
</div>
<div class="section" id="manipulations-sur-les-classes-et-les-objets">
<h2>Manipulations sur les classes et les objets<a class="headerlink" href="#manipulations-sur-les-classes-et-les-objets" title="Lien permanent vers ce titre"></a></h2>
<p>En python un type et une classe cest la même chose. Une classe
est un type standard. En python tout est objet, et tout provient dun seul objet
(qui sappelle <code class="docutils literal"><span class="pre">object</span></code>).</p>
<ul class="simple">
<li>encapsulation (cacher les attributs (variables détat)) dun objet.</li>
<li>interfaces : chaque aspect dune classe peut être vu comme une interface.</li>
</ul>
<p>Une interface décrit un ensemble de comportements.
on peut considérer une interface comme un protocole dutilisation dun objet
dans un contexte donné.</p>
<p>on peut alors créer des outils qui sauront traiter nimporte quel objet
pourvu quil respecte une ensemble dinterfaces.</p>
<div class="admonition-todo admonition" id="index-0">
<p class="first admonition-title">À faire</p>
<p class="last">travailler lhéritage, laggrégation, la délégation</p>
</div>
<p>Voici un exemple de classe <cite>Voiture</cite> :</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Voiture</span><span class="p">(</span><span class="n">Prix</span><span class="p">,</span> <span class="n">Turbo</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">constructeur</span><span class="p">,</span> <span class="n">vitesse_max</span><span class="o">=</span><span class="mi">160</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">constructeur</span> <span class="o">=</span> <span class="n">constructeur</span>
<span class="bp">self</span><span class="o">.</span><span class="n">vitesse_max</span> <span class="o">=</span> <span class="n">vitesse_max</span>
<span class="k">def</span> <span class="nf">roule</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">&quot;vroummm&quot;</span>
<span class="k">def</span> <span class="nf">signaletique</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">&quot;constructeur : </span><span class="si">{0}</span><span class="s2">, vitesse_max </span><span class="si">{1}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">constructeur</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">vitesse_max</span><span class="p">)</span>
</pre></div>
</div>
<p>jinstancie ma classe <cite>Voiture</cite> :</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ma_voiture</span> <span class="o">=</span> <span class="n">Voiture</span><span class="p">(</span><span class="s2">&quot;ma_marque&quot;</span><span class="p">,</span> <span class="s2">&quot;150km/h&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ma_voiture</span><span class="o">.</span><span class="n">roule</span><span class="p">()</span>
<span class="go">&#39;vroummm&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ma_voiture</span><span class="o">.</span><span class="n">signaletique</span><span class="p">()</span>
<span class="go">&#39;constructeur : ma_marque, vitesse_max 150km/h&#39;</span>
</pre></div>
</div>
<div class="admonition-todo admonition" id="index-1">
<p class="first admonition-title">À faire</p>
<p class="last">faire des traitements dans linit</p>
</div>
<ul class="simple">
<li>héritage (est une sorte de)</li>
<li>polymorphisme : un objet apparait comme une instance dune classe parente</li>
</ul>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Turbo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">turbo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">&quot;VRRRRROUUUMMM&quot;</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">=</span> <span class="n">DoDoche</span><span class="p">(</span><span class="s2">&quot;marque&quot;</span><span class="p">,</span> <span class="mi">160</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span><span class="o">.</span><span class="n">get_prix</span><span class="p">()</span>
<span class="go">&#39;7000&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">Prix</span><span class="p">)</span>
<span class="go">True</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>mais lobjet conserve son identité :</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">v</span><span class="p">)</span>
<span class="go">&lt;type &#39;Voiture&#39;&gt;</span>
</pre></div>
</div>
<p>la fonction <code class="docutils literal"><span class="pre">achete_voiture()</span></code> sera appelée indépendamment du type de lobjet,
du moment que lobjet a une méthode <cite>get_prix()</cite>, cest le duck typing, quil
est préférable de ramener au polymorphisme dobjet, ou bien utiliser les <a class="reference internal" href="stdlib.html#module-abc" title="abc: les abstract base classes"><code class="xref py py-mod docutils literal"><span class="pre">abc</span></code></a>
(abstract base classes).</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">achete_voiture</span><span class="p">(</span><span class="n">voiture</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">voiture</span><span class="p">,</span> <span class="s2">&quot;get_prix&quot;</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;pas le bon type&quot;</span><span class="p">)</span>
<span class="k">return</span> <span class="s2">&quot;prix de la voiture: </span><span class="si">{0}</span><span class="s2"> euros&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">voiture</span><span class="o">.</span><span class="n">get_prix</span><span class="p">())</span>
</pre></div>
</div>
<p>tout le code :</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Turbo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">turbo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">&quot;VRRRRROUUUMMM&quot;</span>
<span class="k">class</span> <span class="nc">Prix</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">get_prix</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
<span class="k">class</span> <span class="nc">Voiture</span><span class="p">(</span><span class="n">Prix</span><span class="p">,</span> <span class="n">Turbo</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">constructeur</span><span class="p">,</span> <span class="n">vitesse_max</span><span class="o">=</span><span class="mi">160</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">constructeur</span> <span class="o">=</span> <span class="n">constructeur</span>
<span class="bp">self</span><span class="o">.</span><span class="n">vitesse_max</span> <span class="o">=</span> <span class="n">vitesse_max</span>
<span class="k">def</span> <span class="nf">roule</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">&quot;vroummm&quot;</span>
<span class="k">def</span> <span class="nf">signaletique</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">&quot;constructeur : </span><span class="si">{0}</span><span class="s2">, vitesse_max </span><span class="si">{1}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">constructeur</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">vitesse_max</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">DoDoche</span><span class="p">(</span><span class="n">Voiture</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">get_prix</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">&quot;4000&quot;</span>
<span class="k">def</span> <span class="nf">achete_voiture</span><span class="p">(</span><span class="n">voiture</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">voiture</span><span class="p">,</span> <span class="s2">&quot;get_prix&quot;</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;pas le bon type&quot;</span><span class="p">)</span>
<span class="k">return</span> <span class="s2">&quot;prix de la voiture: </span><span class="si">{0}</span><span class="s2"> euros&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">voiture</span><span class="o">.</span><span class="n">get_prix</span><span class="p">())</span>
</pre></div>
</div>
<p><a class="reference download internal" href="_downloads/heritage.py" download=""><code class="xref download docutils literal"><span class="pre">télécharger</span> <span class="pre">le</span> <span class="pre">code</span></code></a></p>
<ul class="simple">
<li><strong>laggrégation</strong></li>
</ul>
<p>un attribut est lui-même un objet (ce qui est fréquent en python)…</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
<span class="k">pass</span>
<span class="k">class</span> <span class="nc">B</span><span class="p">:</span>
<span class="k">pass</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="n">a</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="n">B</span><span class="p">()</span>
</pre></div>
</div>
<ul class="simple">
<li><strong>la délégation</strong></li>
</ul>
<p>la fonction «&nbsp;property&nbsp;» est un élément du design de python lui-même</p>
<dl class="function">
<dt id="property">
<code class="descname">property</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#property" title="Lien permanent vers cette définition"></a></dt>
<dd></dd></dl>
</div>
<div class="section" id="les-patrons-de-conception">
<h2>les patrons de conception<a class="headerlink" href="#les-patrons-de-conception" title="Lien permanent vers ce titre"></a></h2>
<ul class="simple">
<li>le patron <strong>factory</strong></li>
</ul>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">NotFoundError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">class</span> <span class="nc">MaClasse</span><span class="p">:</span>
<span class="k">pass</span>
<span class="k">class</span> <span class="nc">MaClasseDeux</span><span class="p">:</span>
<span class="k">pass</span>
<span class="n">binding</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">un</span><span class="o">=</span><span class="n">MaClasse</span><span class="p">,</span> <span class="n">deux</span><span class="o">=</span><span class="n">MaClasseDeux</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">ma_factory</span><span class="p">(</span><span class="n">key</span><span class="p">):</span>
<span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">binding</span><span class="p">:</span>
<span class="k">return</span> <span class="n">binding</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="k">return</span> <span class="n">NotFoundError</span><span class="p">(</span><span class="s2">&quot;keskece?&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p><a class="reference download internal" href="_downloads/patrons.py" download=""><code class="xref download docutils literal"><span class="pre">télécharger</span> <span class="pre">usine</span> <span class="pre">(factory)</span></code></a></p>
<ul class="simple">
<li>le patron <strong>wrapper</strong></li>
</ul>
<p><a class="reference download internal" href="_downloads/wrap.py" download=""><code class="xref download docutils literal"><span class="pre">télécharger</span> <span class="pre">wrapper</span></code></a></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Wrap</span><span class="p">(</span><span class="nb">object</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">name</span><span class="p">,</span> <span class="n">wrap</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">slots</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;_name&#39;</span><span class="p">,</span> <span class="s1">&#39;_w&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="o">=</span> <span class="n">name</span> <span class="ow">or</span> <span class="s2">&quot;wrapped_element&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_w</span> <span class="o">=</span> <span class="n">wrap</span>
<span class="k">def</span> <span class="nf">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
<span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">:</span>
<span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_w</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
<span class="c1"># def get_w(self, name):</span>
<span class="c1"># return getattr(self._w, name)</span>
<span class="c1"># def set_w(self, name, value):</span>
<span class="c1"># return setattr(self._w, name, value)</span>
<span class="c1"># _w = property (get_w, set_w)</span>
<span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">&quot;[W_Element </span><span class="si">%s</span><span class="s2">]&quot;</span><span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_name</span><span class="p">)</span>
</pre></div>
</div>
<p>exemple dutilisation de <cite>Wrap()</cite></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">O</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">o</span> <span class="o">=</span> <span class="n">O</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">o</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="s2">&quot;blah&quot;</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">wrap</span> <span class="k">import</span> <span class="n">Wrap</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">Wrap</span><span class="p">(</span><span class="s2">&quot;monwrap&quot;</span><span class="p">,</span> <span class="n">o</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">_name</span>
<span class="go">&#39;monwrap&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">_w</span>
<span class="go">&lt;__main__.O instance at 0x7fbf177aaa28&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">_w</span><span class="o">.</span><span class="n">a</span>
<span class="go">&#39;blah&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">a</span>
<span class="go">&#39;blah&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">u</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
File <span class="nb">&quot;wrap.py&quot;</span>, line <span class="m">11</span>, in <span class="n">__getattr__</span>
<span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_w</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
<span class="gr">AttributeError</span>: <span class="n">O instance has no attribute &#39;u&#39;</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<ul class="simple">
<li>le patron de conception <strong>itérable</strong></li>
</ul>
<p><a class="reference download internal" href="_downloads/iterable.py" download=""><code class="xref download docutils literal"><span class="pre">télécharger</span> <span class="pre">iterable</span></code></a></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">liste</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;blah&#39;</span><span class="p">,</span> <span class="s1">&#39;blih&#39;</span><span class="p">,</span> <span class="s1">&#39;bluh&#39;</span><span class="p">]</span>
<span class="n">iterateur</span> <span class="o">=</span> <span class="n">liste</span><span class="o">.</span><span class="fm">__iter__</span><span class="p">()</span>
<span class="nb">print</span> <span class="n">iterateur</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
<span class="nb">print</span> <span class="n">iterateur</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
<span class="nb">print</span> <span class="n">iterateur</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
<span class="nb">print</span> <span class="n">iterateur</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
<span class="c1">#Traceback (most recent call last):</span>
<span class="c1"># File &#39;&lt;stdin&gt;&#39;, line 1, in &lt;module&gt;;</span>
<span class="c1">#StopIteration</span>
<span class="k">class</span> <span class="nc">Iterateur</span><span class="p">:</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">10</span><span class="p">:</span> <span class="k">raise</span> <span class="ne">StopIteration</span>
<span class="bp">self</span><span class="o">.</span><span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">return</span> <span class="mi">2</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">i</span>
<span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span>
<span class="n">iterateur</span> <span class="o">=</span> <span class="n">Iterateur</span><span class="p">()</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">iterateur</span><span class="p">:</span> <span class="nb">print</span> <span class="n">i</span>
</pre></div>
</div>
<ul class="simple">
<li>le patron <strong>decorateur</strong></li>
</ul>
<p><a class="reference download internal" href="_downloads/decorateur.py" download=""><code class="xref download docutils literal"><span class="pre">télécharger</span> <span class="pre">decorateur</span></code></a></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">helloworld</span><span class="p">(</span><span class="n">ob</span><span class="p">):</span>
<span class="nb">print</span> <span class="s2">&quot;Hello world&quot;</span>
<span class="k">return</span> <span class="n">ob</span>
<span class="nd">@helloworld</span>
<span class="k">def</span> <span class="nf">myfunc</span><span class="p">():</span>
<span class="nb">print</span> <span class="s2">&quot;my function&quot;</span>
<span class="n">myfunc</span><span class="p">()</span>
<span class="nb">print</span> <span class="n">myfunc</span>
</pre></div>
</div>
<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">deco</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">func</span><span class="o">.</span><span class="n">attr</span> <span class="o">=</span> <span class="s1">&#39;decorated&#39;</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">func</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nd">@deco</span>
<span class="gp">... </span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">attr</span>
<span class="go">&#39;decorated&#39;</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>autre exemple : les méthodes statiques</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="nd">@staticmethod</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">my_class_method</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
<span class="gp">... </span> <span class="c1"># do stuff here</span>
</pre></div>
</div>
</div>
<div class="section" id="metaclasses">
<h2>métaclasses<a class="headerlink" href="#metaclasses" title="Lien permanent vers ce titre"></a></h2>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&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;type &#39;classobj&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">B</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">B</span><span class="p">)</span>
<span class="go">&lt;type &#39;type&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">help</span><span class="p">(</span><span class="nb">type</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">C</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;C&#39;</span><span class="p">,</span> <span class="p">(),</span> <span class="p">{})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span>
<span class="go">&lt;class &#39;__main__.C&#39;&gt;</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<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="nb">object</span><span class="p">)</span>
<span class="go">&lt;type &#39;type&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="nb">type</span><span class="p">)</span>
<span class="go">&lt;type &#39;type&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="nb">type</span><span class="p">,</span> <span class="nb">object</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="nb">object</span><span class="p">,</span> <span class="nb">type</span><span class="p">)</span>
<span class="go">True</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MaMetaClasse</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Exemple d&#39;une métaclasse.&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">metacls</span><span class="p">,</span> <span class="n">nom</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Création de notre classe.&quot;&quot;&quot;</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;On crée la classe </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">nom</span><span class="p">))</span>
<span class="k">return</span> <span class="nb">type</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="n">metacls</span><span class="p">,</span> <span class="n">nom</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="nb">dict</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MaClasse</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">MaMetaClasse</span>
</pre></div>
</div>
<p>exemple : forcer limplémentation dun singleton avec les métaclasses</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Singleton</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
<span class="n">instance</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">cls</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">kw</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="bp">cls</span><span class="o">.</span><span class="n">instance</span><span class="p">:</span>
<span class="bp">cls</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">Singleton</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span><span class="o">.</span><span class="fm">__call__</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">kw</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">instance</span>
<span class="k">class</span> <span class="nc">ASingleton</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">Singleton</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">ASingleton</span><span class="p">()</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">ASingleton</span><span class="p">()</span>
<span class="k">assert</span> <span class="n">a</span> <span class="ow">is</span> <span class="n">b</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">BSingleton</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">Singleton</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">BSingleton</span><span class="p">()</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">BSingleton</span><span class="p">()</span>
<span class="k">assert</span> <span class="n">c</span> <span class="ow">is</span> <span class="n">d</span>
<span class="nb">print</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">c</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">a</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<a href="search.html"> Recherche</a> | &nbsp;
<!-- a href="genindex.html"> Genindex</a-->
</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="Index général"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Index des modules Python"
>modules</a> |</li>
<li class="right" >
<a href="prompt.html" title="Interactions avec lutilisateur"
>suivant</a> |</li>
<li class="right" >
<a href="docutils.html" title="Sphinx et docutils"
>précédent</a> |</li>
<a href="index.html">Programmation python </a> &raquo;
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2015, cadoles (www.cadoles.com).
Créé avec <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.7.
</div>
</body>
</html>