676 lines
56 KiB
HTML
676 lines
56 KiB
HTML
|
||
<!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 — 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 l’utilisateur" 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> 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 l’utilisateur"
|
||
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> »
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<p> </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 d’un objet</strong> : ensemble de propriétés auxquelles sont associées des
|
||
valeurs.</p>
|
||
<p>Les variables de l’objet sont appelées des <strong>attributs</strong>.
|
||
le comportement d’un <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 l’objet</li>
|
||
<li>des appels faits sur l’objet</li>
|
||
</ul>
|
||
<p>envois de messages à l’objet = 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 d’un objet sont définis par sa classe</li>
|
||
<li>une classe possède un ensemble d’attributs 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 « est une espèce de » utilisée entre une classe et une autre classe</dd>
|
||
<dt id="term-instantiation">instantiation</dt>
|
||
<dd>relation « est une instance de » 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 d’une classe</li>
|
||
<li>instance de classe : on peut créer des objets à partir d’un type « classe »
|
||
(une classe est instanciable)</li>
|
||
</ul>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="k">pass</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </span><span class="n">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">>>> </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">>>> </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">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">B</span><span class="p">()</span>
|
||
<span class="gp">>>> </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">>>> </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 d’hé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 d’objets 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">>>> </span><span class="n">o</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span>
|
||
<span class="gp">>>> </span><span class="n">o</span>
|
||
<span class="go"><object object at 0x7f77c9cda0d0></span>
|
||
<span class="gp">>>> </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">>>> </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">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
|
||
<span class="gp">>>> </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">>>> </span><span class="n">c</span><span class="o">.</span><span class="n">a</span>
|
||
<span class="go">3</span>
|
||
<span class="gp">>>> </span><span class="nb">vars</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
|
||
<span class="go">{'a': 3}</span>
|
||
<span class="gp">>>> </span><span class="n">c</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{'a': 3}</span>
|
||
<span class="gp">>>> </span><span class="n">C</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{'__module__': '__main__', '__doc__': None}</span>
|
||
<span class="gp">>>> </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">>>> </span><span class="n">C</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{'c': 5, '__module__': '__main__', '__doc__': None}</span>
|
||
<span class="gp">>>> </span><span class="n">c</span><span class="o">.</span><span class="n">c</span>
|
||
<span class="go">5</span>
|
||
<span class="gp">>>> </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">>>> </span><span class="n">c</span><span class="o">.</span><span class="n">z</span>
|
||
<span class="go">3</span>
|
||
<span class="gp">>>> </span><span class="n">c</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{'a': 3, 'z': 3}</span>
|
||
<span class="gp">>>> </span><span class="n">C</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{'c': 5, '__module__': '__main__', '__doc__': None}</span>
|
||
<span class="gp">>>> </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">>>> </span><span class="n">MaKlass</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{'__module__': '__main__', '__doc__': None, 'unefonction': <function unefonction at 0x7f77c5b0c488>}</span>
|
||
<span class="gp">>>> </span><span class="n">k</span> <span class="o">=</span> <span class="n">MaKlass</span><span class="p">()</span>
|
||
<span class="gp">>>> </span><span class="n">k</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{}</span>
|
||
<span class="gp">>>> </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">"<stdin>"</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">>>> </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">>>> </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">>>> </span><span class="n">k</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{'autrefonc': <function autrefonc at 0x7f77c5b0c500>}</span>
|
||
<span class="gp">>>> </span><span class="n">MaKlass</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{'__module__': '__main__', '__doc__': None, 'unefonction': <function unefonction at 0x7f77c5b0c488>}</span>
|
||
<span class="gp">>>> </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">"toto"</span><span class="p">)</span>
|
||
<span class="go">toto</span>
|
||
<span class="gp">>>> </span><span class="n">k</span><span class="o">.</span><span class="n">unefonction</span><span class="p">(</span><span class="s2">"toto"</span><span class="p">)</span>
|
||
<span class="go">toto</span>
|
||
<span class="gp">>>> </span><span class="n">k</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{'autrefonc': <function autrefonc at 0x7f77c5b0c500>}</span>
|
||
<span class="gp">>>> </span><span class="n">MaKlass</span><span class="o">.</span><span class="n">toto</span> <span class="o">=</span> <span class="s2">"test"</span>
|
||
<span class="gp">>>> </span><span class="n">k</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{'autrefonc': <function autrefonc at 0x7f77c5b0c500>}</span>
|
||
<span class="gp">>>> </span><span class="n">k</span><span class="o">.</span><span class="n">toto</span>
|
||
<span class="go">'test'</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="le-dict-avec-l-heritage-de-classe">
|
||
<h2>le __dict__ avec l’hé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">>>> </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">>>> </span><span class="n">A</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">dict_proxy({'__dict__': <attribute '__dict__' of 'A' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None})</span>
|
||
<span class="gp">>>> </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">>>> </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">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
|
||
<span class="gp">>>> </span><span class="n">o</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
|
||
<span class="gp">>>> </span><span class="n">o</span><span class="o">.</span><span class="vm">__dict__</span>
|
||
<span class="go">{}</span>
|
||
<span class="gp">>>> </span><span class="n">o</span><span class="o">.</span><span class="n">c</span>
|
||
<span class="go">2</span>
|
||
<span class="gp">>>> </span><span class="n">o</span><span class="o">.</span><span class="n">b</span>
|
||
<span class="go">3</span>
|
||
<span class="gp">>>> </span><span class="n">o</span><span class="o">.</span><span class="vm">__class__</span>
|
||
<span class="go"><class '__main__.C'></span>
|
||
<span class="gp">>>> </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({'__module__': '__main__', 'c': 2, '__doc__': None})</span>
|
||
<span class="go">>>></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">>>> </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">>>> </span><span class="n">A</span><span class="o">.</span><span class="vm">__mro__</span>
|
||
<span class="go">(<class '__main__.A'>, <type 'object'>)</span>
|
||
<span class="gp">>>> </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">>>> </span><span class="n">B</span><span class="o">.</span><span class="vm">__mro__</span>
|
||
<span class="go">(<class '__main__.B'>, <class '__main__.A'>, <type 'object'>)</span>
|
||
<span class="gp">>>> </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">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></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">>>></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. C’est plus <strong>agile</strong>.</p>
|
||
<p>attributs et méthodes vus comme des ajouts dans l’espace de nommage</p>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </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">>>> </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">>>> </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">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">f</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">)</span>
|
||
<span class="go">hello</span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="la-necessite-d-un-design-objet">
|
||
<h3>la nécessité d’un design objet<a class="headerlink" href="#la-necessite-d-un-design-objet" title="Lien permanent vers ce titre">¶</a></h3>
|
||
<p>affichage d’une 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> – l’objet 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 c’est la même chose. Une classe
|
||
est un type standard. En python tout est objet, et tout provient d’un seul objet
|
||
(qui s’appelle <code class="docutils literal"><span class="pre">object</span></code>).</p>
|
||
<ul class="simple">
|
||
<li>encapsulation (cacher les attributs (variables d’état)) d’un objet.</li>
|
||
<li>interfaces : chaque aspect d’une 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 d’utilisation d’un objet
|
||
dans un contexte donné.</p>
|
||
<p>on peut alors créer des outils qui sauront traiter n’importe quel objet
|
||
pourvu qu’il respecte une ensemble d’interfaces.</p>
|
||
<div class="admonition-todo admonition" id="index-0">
|
||
<p class="first admonition-title">À faire</p>
|
||
<p class="last">travailler l’héritage, l’aggré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">"vroummm"</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">"constructeur : </span><span class="si">{0}</span><span class="s2">, vitesse_max </span><span class="si">{1}</span><span class="s2">"</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>j’instancie ma classe <cite>Voiture</cite> :</p>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">ma_voiture</span> <span class="o">=</span> <span class="n">Voiture</span><span class="p">(</span><span class="s2">"ma_marque"</span><span class="p">,</span> <span class="s2">"150km/h"</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">ma_voiture</span><span class="o">.</span><span class="n">roule</span><span class="p">()</span>
|
||
<span class="go">'vroummm'</span>
|
||
<span class="gp">>>> </span><span class="n">ma_voiture</span><span class="o">.</span><span class="n">signaletique</span><span class="p">()</span>
|
||
<span class="go">'constructeur : ma_marque, vitesse_max 150km/h'</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 l’init</p>
|
||
</div>
|
||
<ul class="simple">
|
||
<li>héritage (est une sorte de)</li>
|
||
<li>polymorphisme : un objet apparait comme une instance d’une 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">"VRRRRROUUUMMM"</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">v</span> <span class="o">=</span> <span class="n">DoDoche</span><span class="p">(</span><span class="s2">"marque"</span><span class="p">,</span> <span class="mi">160</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">v</span><span class="o">.</span><span class="n">get_prix</span><span class="p">()</span>
|
||
<span class="go">'7000'</span>
|
||
<span class="gp">>>> </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">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>mais l’objet conserve son identité :</p>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">type</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
|
||
<span class="go"><type 'Voiture'></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 l’objet,
|
||
du moment que l’objet a une méthode <cite>get_prix()</cite>, c’est le duck typing, qu’il
|
||
est préférable de ramener au polymorphisme d’objet, 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">"get_prix"</span><span class="p">):</span>
|
||
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">"pas le bon type"</span><span class="p">)</span>
|
||
<span class="k">return</span> <span class="s2">"prix de la voiture: </span><span class="si">{0}</span><span class="s2"> euros"</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">"VRRRRROUUUMMM"</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">"vroummm"</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">"constructeur : </span><span class="si">{0}</span><span class="s2">, vitesse_max </span><span class="si">{1}</span><span class="s2">"</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">"4000"</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">"get_prix"</span><span class="p">):</span>
|
||
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">"pas le bon type"</span><span class="p">)</span>
|
||
<span class="k">return</span> <span class="s2">"prix de la voiture: </span><span class="si">{0}</span><span class="s2"> euros"</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>l’aggré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 « property » 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">"keskece?"</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">'_name'</span><span class="p">,</span> <span class="s1">'_w'</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">"wrapped_element"</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">"[W_Element </span><span class="si">%s</span><span class="s2">]"</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 d’utilisation de <cite>Wrap()</cite></p>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </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">>>> </span><span class="n">o</span> <span class="o">=</span> <span class="n">O</span><span class="p">()</span>
|
||
<span class="gp">>>> </span><span class="n">o</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="s2">"blah"</span>
|
||
<span class="go">>>></span>
|
||
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">wrap</span> <span class="k">import</span> <span class="n">Wrap</span>
|
||
<span class="gp">>>> </span><span class="n">w</span> <span class="o">=</span> <span class="n">Wrap</span><span class="p">(</span><span class="s2">"monwrap"</span><span class="p">,</span> <span class="n">o</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">w</span><span class="o">.</span><span class="n">_name</span>
|
||
<span class="go">'monwrap'</span>
|
||
<span class="gp">>>> </span><span class="n">w</span><span class="o">.</span><span class="n">_w</span>
|
||
<span class="go"><__main__.O instance at 0x7fbf177aaa28></span>
|
||
<span class="gp">>>> </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">'blah'</span>
|
||
<span class="gp">>>> </span><span class="n">w</span><span class="o">.</span><span class="n">a</span>
|
||
<span class="go">'blah'</span>
|
||
<span class="gp">>>> </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">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
|
||
File <span class="nb">"wrap.py"</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 'u'</span>
|
||
<span class="go">>>></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">'blah'</span><span class="p">,</span> <span class="s1">'blih'</span><span class="p">,</span> <span class="s1">'bluh'</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 '<stdin>', line 1, in <module>;</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"><=</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">"Hello world"</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">"my function"</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">>>> </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">'decorated'</span>
|
||
<span class="gp">... </span> <span class="k">return</span> <span class="n">func</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </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">>>> </span><span class="n">f</span><span class="o">.</span><span class="n">attr</span>
|
||
<span class="go">'decorated'</span>
|
||
<span class="go">>>></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">>>> </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">>>> </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="k">pass</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </span><span class="nb">type</span><span class="p">(</span><span class="n">A</span><span class="p">)</span>
|
||
<span class="go"><type 'classobj'></span>
|
||
<span class="gp">>>> </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">>>> </span><span class="nb">type</span><span class="p">(</span><span class="n">B</span><span class="p">)</span>
|
||
<span class="go"><type 'type'></span>
|
||
<span class="gp">>>> </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">>>> </span><span class="n">C</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="s1">'C'</span><span class="p">,</span> <span class="p">(),</span> <span class="p">{})</span>
|
||
<span class="gp">>>> </span><span class="n">C</span>
|
||
<span class="go"><class '__main__.C'></span>
|
||
<span class="go">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">type</span><span class="p">(</span><span class="nb">object</span><span class="p">)</span>
|
||
<span class="go"><type 'type'></span>
|
||
<span class="gp">>>> </span><span class="nb">type</span><span class="p">(</span><span class="nb">type</span><span class="p">)</span>
|
||
<span class="go"><type 'type'></span>
|
||
<span class="gp">>>> </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">>>> </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">>>></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">"""Exemple d'une métaclasse."""</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">"""Création de notre classe."""</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"On crée la classe </span><span class="si">{}</span><span class="s2">"</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 l’implémentation d’un 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> |
|
||
<!-- 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 l’utilisateur"
|
||
>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> »
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer" role="contentinfo">
|
||
© Copyright 2015, cadoles (www.cadoles.com).
|
||
Créé avec <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.7.
|
||
</div>
|
||
</body>
|
||
</html> |