359 lines
27 KiB
HTML
359 lines
27 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>Programmation python courante — 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="Le style de programmation par exceptions" href="exceptions.html" />
|
|||
|
<link rel="prev" title="Interactions avec l’utilisateur" href="prompt.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="exceptions.html" title="Le style de programmation par exceptions"
|
|||
|
accesskey="N">suivant</a> |</li>
|
|||
|
<li class="right" >
|
|||
|
<a href="prompt.html" title="Interactions avec l’utilisateur"
|
|||
|
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="programmation-python-courante">
|
|||
|
<h1>Programmation python courante<a class="headerlink" href="#programmation-python-courante" title="Lien permanent vers ce titre">¶</a></h1>
|
|||
|
<div class="section" id="les-espaces-de-nommage">
|
|||
|
<span id="namespaces"></span><h2>les espaces de nommage<a class="headerlink" href="#les-espaces-de-nommage" title="Lien permanent vers ce titre">¶</a></h2>
|
|||
|
<p>L’espace de nommage le plus courant est l’organisation en modules et en packages.</p>
|
|||
|
<p>Packages et modules:</p>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">package</span><span class="o">/</span>
|
|||
|
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
|
|||
|
<span class="n">module1</span><span class="o">.</span><span class="n">py</span>
|
|||
|
<span class="n">subpackage</span><span class="o">/</span>
|
|||
|
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
|
|||
|
<span class="n">module2</span><span class="o">.</span><span class="n">py</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>A utilser pour organiser votre projet
|
|||
|
Permet de minimiser les risques de conflits de nome
|
|||
|
Permet de diminuer les entrées dans le <span class="target" id="index-0"></span><a class="reference internal" href="settings.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONPATH</span></code></a></p>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">package.module1</span>
|
|||
|
<span class="kn">from</span> <span class="nn">package.subpackage</span> <span class="k">import</span> <span class="n">module2</span>
|
|||
|
<span class="kn">from</span> <span class="nn">package.subpackage.module2</span> <span class="k">import</span> <span class="n">name</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<ul class="simple">
|
|||
|
<li>le fichier <cite>__init__.py</cite></li>
|
|||
|
<li><cite>reload(module)</cite> au prompt</li>
|
|||
|
<li>dangereux : l’import « * », utiliser l’attribut spécial <cite>__all__</cite> pour l’import
|
|||
|
sélectif</li>
|
|||
|
</ul>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">os</span> <span class="k">import</span> <span class="o">*</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>lance un module en tant que script :</p>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
|
|||
|
<span class="n">main</span><span class="p">()</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Organisation modulaire</p>
|
|||
|
<ul class="simple">
|
|||
|
<li>construire des composants élémentaires</li>
|
|||
|
<li>combiner ces composants</li>
|
|||
|
<li>utiliser une structure pyramidale : les composants sont les éléments de
|
|||
|
composants plus complexes.</li>
|
|||
|
<li>découplage de l’ensemble en composants indépendants (gros programmes réalisables)</li>
|
|||
|
<li>donner de la structure (rendre les gros programmes compréhensibles)</li>
|
|||
|
<li>spécifier les liens entre les composants (rendre les programmes maintenables)</li>
|
|||
|
<li>identifier les sous-composants indépendants (rendre les programmes réutilisables)</li>
|
|||
|
<li>forcer l’abstraction (augmenter la sureté du programme)</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="section" id="modules-charges-et-modules-importes">
|
|||
|
<h2>modules chargés et modules importés<a class="headerlink" href="#modules-charges-et-modules-importes" title="Lien permanent vers ce titre">¶</a></h2>
|
|||
|
<p>Les modules susceptibles d’être chargés sont dans le <span class="target" id="index-1"></span><a class="reference internal" href="settings.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONPATH</span></code></a>.
|
|||
|
Mais comment peut-on savoir ou ils sont physiquement (sur le disque dur) ?</p>
|
|||
|
<dl class="envvar">
|
|||
|
<dt id="envvar-`sys.modules`">
|
|||
|
<code class="descname">`sys.modules`</code><a class="headerlink" href="#envvar-`sys.modules`" title="Lien permanent vers cette définition">¶</a></dt>
|
|||
|
<dd></dd></dl>
|
|||
|
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'twisted'</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span>
|
|||
|
<span class="go">False</span>
|
|||
|
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">twisted</span>
|
|||
|
<span class="gp">>>> </span><span class="s1">'twisted'</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span>
|
|||
|
<span class="go">True</span>
|
|||
|
<span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="s1">'twisted'</span><span class="p">]</span>
|
|||
|
<span class="go"><module 'twisted' from '/usr/lib/python2.7/dist-packages/twisted/__init__.pyc'></span>
|
|||
|
<span class="go">>>></span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<div class="admonition attention">
|
|||
|
<p class="first admonition-title">Attention</p>
|
|||
|
<p class="last">un module présent dans <cite>sys.modules</cite> n’est pas forcément importé
|
|||
|
dans l’espace de nommage usuel. Il faut importer le module pour
|
|||
|
pouvoir l’utiliser.</p>
|
|||
|
</div>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="s1">'email'</span><span class="p">]</span>
|
|||
|
<span class="go"><module 'email' from '/usr/lib/python2.7/email/__init__.pyc'></span>
|
|||
|
<span class="gp">>>> </span><span class="nb">dir</span><span class="p">(</span><span class="n">email</span><span class="p">)</span>
|
|||
|
<span class="gt">Traceback (most recent call last):</span>
|
|||
|
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
|
|||
|
<span class="gr">NameError</span>: <span class="n">name 'email' is not defined</span>
|
|||
|
<span class="go">>>></span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Pour récupérer le chemin du module</p>
|
|||
|
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="o"><</span><span class="n">module</span><span class="o">>.</span><span class="vm">__file__</span><span class="p">))</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Pour importer un module qui n’est pas dans le <cite>sys.path</cite></p>
|
|||
|
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">fch</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'/path/to/mymodule/custom.py'</span><span class="p">,</span> <span class="s1">'r'</span><span class="p">)</span>
|
|||
|
<span class="n">my_module</span> <span class="o">=</span> <span class="n">imp</span><span class="o">.</span><span class="n">load_module</span><span class="p">(</span><span class="s1">'dhcp_custom'</span><span class="p">,</span> <span class="n">fch</span><span class="p">,</span> <span class="s1">'/path/to/mymodule.py'</span><span class="p">,</span> <span class="p">(</span><span class="s1">'.py'</span><span class="p">,</span> <span class="s1">'U'</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="connaitre-la-version-d-un-module">
|
|||
|
<h2>Connaître la version d’un module<a class="headerlink" href="#connaitre-la-version-d-un-module" title="Lien permanent vers ce titre">¶</a></h2>
|
|||
|
<p>Exemple : le module <code class="docutils literal"><span class="pre">datetime</span></code></p>
|
|||
|
<p>C’est suivant la version de python car c’est la librairie standard.</p>
|
|||
|
<p>Sinon, en général il y a un attribut __version__</p>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlalchemy</span>
|
|||
|
<span class="gp">>>> </span><span class="n">sqlalchemy</span><span class="o">.</span><span class="n">__version__</span>
|
|||
|
<span class="go">'0.9.8'</span>
|
|||
|
<span class="go">>>></span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="les-methodes-speciales">
|
|||
|
<h2>Les méthodes spéciales<a class="headerlink" href="#les-methodes-speciales" title="Lien permanent vers ce titre">¶</a></h2>
|
|||
|
<p>méthodes spéciales correspondants aux interfaces des types de bases :</p>
|
|||
|
<dl class="function">
|
|||
|
<dt id="__init__">
|
|||
|
<code class="descname">__init__</code><span class="sig-paren">(</span><em>self</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#__init__" title="Lien permanent vers cette définition">¶</a></dt>
|
|||
|
<dt>
|
|||
|
<code class="descname">le constructeur de l'instance d'objet</code></dt>
|
|||
|
<dd></dd></dl>
|
|||
|
|
|||
|
<dl class="function">
|
|||
|
<dt id="__add__">
|
|||
|
<code class="descname">__add__</code><span class="sig-paren">(</span><em>self</em>, <em>other</em><span class="sig-paren">)</span><a class="headerlink" href="#__add__" title="Lien permanent vers cette définition">¶</a></dt>
|
|||
|
<dd><p>correspond à la notation <cite>+</cite></p>
|
|||
|
</dd></dl>
|
|||
|
|
|||
|
<p>exemple :</p>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Zone</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">level</span><span class="o">=</span><span class="mi">0</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="bp">self</span><span class="o">.</span><span class="n">level</span> <span class="o">=</span> <span class="n">level</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">__add__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="n">Zone</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">other</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">level</span><span class="o">+</span><span class="n">other</span><span class="o">.</span><span class="n">level</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">__cmp__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="nb">cmp</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">level</span><span class="p">,</span> <span class="n">other</span><span class="o">.</span><span class="n">level</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">specialmethods</span> <span class="k">import</span> <span class="o">*</span>
|
|||
|
<span class="gp">>>> </span><span class="n">z</span> <span class="o">=</span> <span class="n">Zone</span><span class="p">(</span><span class="s2">"titi"</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">z2</span> <span class="o">=</span> <span class="n">Zone</span><span class="p">(</span><span class="s2">"tutu"</span><span class="p">,</span> <span class="mi">40</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">z</span> <span class="o">></span> <span class="n">z2</span>
|
|||
|
<span class="go">False</span>
|
|||
|
<span class="gp">>>> </span><span class="n">z</span> <span class="o">+</span> <span class="n">z2</span>
|
|||
|
<span class="go"><specialmethods.Zone object at 0x7f02d95fb190></span>
|
|||
|
<span class="gp">>>> </span><span class="n">z3</span> <span class="o">=</span> <span class="n">z</span> <span class="o">+</span> <span class="n">z2</span>
|
|||
|
<span class="gp">>>> </span><span class="n">z3</span><span class="o">.</span><span class="n">name</span>
|
|||
|
<span class="go">'tititutu'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">z3</span><span class="o">.</span><span class="n">level</span>
|
|||
|
<span class="go">50</span>
|
|||
|
<span class="go">>>></span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="attributs-et-accesseurs">
|
|||
|
<h2>Attributs et accesseurs<a class="headerlink" href="#attributs-et-accesseurs" title="Lien permanent vers ce titre">¶</a></h2>
|
|||
|
<p>python est un langage à attributs, c’est-à-dire que le protocole d’accès
|
|||
|
aux attributs est règlable.</p>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></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="gp">... </span> <span class="n">classattr</span> <span class="o">=</span> <span class="s2">"a class attribute"</span>
|
|||
|
<span class="gp">...</span>
|
|||
|
<span class="gp">>>> </span><span class="n">cobj</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </span><span class="n">cobj</span><span class="o">.</span><span class="n">classattr</span>
|
|||
|
<span class="go">'a class attribute'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">cobj</span><span class="o">.</span><span class="n">insattr</span> <span class="o">=</span> <span class="s2">"an instance attribute"</span>
|
|||
|
<span class="gp">>>> </span><span class="n">cobj</span><span class="o">.</span><span class="n">insattr</span>
|
|||
|
<span class="go">'an instance attribute'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">C</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">'classattr'</span><span class="p">]</span>
|
|||
|
<span class="go">'a class attribute'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">cobj</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">'insattr'</span><span class="p">]</span>
|
|||
|
<span class="go">'an instance attribute'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>les attributs ne sont pas systématiquement encapsulées en python.</p>
|
|||
|
<p>pour contrôler l’accès aux attributs, on utilise les méthodes spéciales:</p>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="fm">__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="fm">__setattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">class</span> <span class="nc">AnObject</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|||
|
<span class="o">......</span>
|
|||
|
<span class="k">def</span> <span class="nf">__setattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
|
|||
|
<span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s1">'src'</span><span class="p">:</span> <span class="c1">#do something</span>
|
|||
|
<span class="c1"># this will assign the real object.name,</span>
|
|||
|
<span class="c1">#despite __setattr__</span>
|
|||
|
<span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="n">name</span><span class="p">]</span><span class="o">=</span><span class="n">val</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="c1"># ...</span>
|
|||
|
<span class="k">try</span><span class="p">:</span>
|
|||
|
<span class="n">func</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s2">"method"</span><span class="p">)</span>
|
|||
|
<span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
|
|||
|
<span class="o">...</span> <span class="n">deal</span> <span class="k">with</span> <span class="n">missing</span> <span class="n">method</span> <span class="o">...</span>
|
|||
|
<span class="k">else</span><span class="p">:</span>
|
|||
|
<span class="n">result</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="n">func</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s2">"method"</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
|
|||
|
<span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
|
|||
|
<span class="n">func</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<ul class="simple">
|
|||
|
<li>un <strong>attribut</strong> spécial : <cite>__slots__</cite></li>
|
|||
|
</ul>
|
|||
|
<p>permet de fixer les attributs possibles d’une classe</p>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Bar</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s2">"a"</span><span class="p">,</span><span class="s2">"b"</span><span class="p">,</span><span class="s2">"c"</span><span class="p">)</span>
|
|||
|
<span class="gp">...</span>
|
|||
|
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">Bar</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </span><span class="n">b</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="s2">"toto"</span>
|
|||
|
<span class="gp">>>> </span><span class="n">b</span><span class="o">.</span><span class="n">a</span>
|
|||
|
<span class="go">'toto'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">b</span><span class="o">.</span><span class="n">d</span> <span class="o">=</span> <span class="s2">"titi"</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">?</span>
|
|||
|
<span class="gr">AttributeError</span>: <span class="n">'Bar' object has no attribute 'd'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<div class="section" id="les-slots">
|
|||
|
<h3>les slots<a class="headerlink" href="#les-slots" title="Lien permanent vers ce titre">¶</a></h3>
|
|||
|
<div class="admonition important">
|
|||
|
<p class="first admonition-title">Important</p>
|
|||
|
<p class="last">l’encapsulation n’est pas une condition de base de la programmation
|
|||
|
par objects, surtout que le contrôle nuit à l’agilité.</p>
|
|||
|
</div>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Point</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="vm">__slots__</span> <span class="o">=</span> <span class="s1">'x'</span><span class="p">,</span> <span class="s1">'y'</span>
|
|||
|
<span class="gp">...</span>
|
|||
|
<span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
|
|||
|
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="mi">3</span>
|
|||
|
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">z</span> <span class="o">=</span> <span class="mi">4</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">AttributeError</span>: <span class="n">'Point' object has no attribute 'z'</span>
|
|||
|
<span class="go">>>></span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<ul class="simple">
|
|||
|
<li>notation <cite>|</cite> et notation <cite>></cite></li>
|
|||
|
</ul>
|
|||
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Test</span><span class="p">:</span>
|
|||
|
<span class="n">nombre</span> <span class="o">=</span> <span class="mi">1</span>
|
|||
|
<span class="k">def</span> <span class="nf">__or__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">nombre</span> <span class="o">+</span> <span class="n">other</span><span class="o">.</span><span class="n">nombre</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">__lshift__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
|
|||
|
<span class="bp">self</span><span class="o">.</span><span class="n">nombre</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">nombre</span> <span class="o">+</span> <span class="n">other</span><span class="o">.</span><span class="n">nombre</span>
|
|||
|
|
|||
|
<span class="n">t1</span> <span class="o">=</span> <span class="n">Test</span><span class="p">()</span>
|
|||
|
<span class="n">t2</span> <span class="o">=</span> <span class="n">Test</span><span class="p">()</span>
|
|||
|
<span class="n">t2</span><span class="o">.</span><span class="n">nombre</span> <span class="o">=</span> <span class="mi">2</span>
|
|||
|
<span class="nb">print</span> <span class="n">t1</span> <span class="o">|</span> <span class="n">t2</span>
|
|||
|
<span class="n">t1</span> <span class="o"><<</span> <span class="n">t2</span>
|
|||
|
<span class="nb">print</span> <span class="n">t1</span><span class="o">.</span><span class="n">nombre</span>
|
|||
|
</pre></div>
|
|||
|
</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="exceptions.html" title="Le style de programmation par exceptions"
|
|||
|
>suivant</a> |</li>
|
|||
|
<li class="right" >
|
|||
|
<a href="prompt.html" title="Interactions avec l’utilisateur"
|
|||
|
>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>
|