formations/python/python3/fr/_build/html/specialmethods.html

359 lines
27 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>Programmation python courante &#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="Le style de programmation par exceptions" href="exceptions.html" />
<link rel="prev" title="Interactions avec lutilisateur" 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>&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="exceptions.html" title="Le style de programmation par exceptions"
accesskey="N">suivant</a> |</li>
<li class="right" >
<a href="prompt.html" title="Interactions avec lutilisateur"
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="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>Lespace de nommage le plus courant est lorganisation 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 : limport «&nbsp;*&nbsp;», utiliser lattribut spécial <cite>__all__</cite> pour limport
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">&quot;__main__&quot;</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 lensemble 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 labstraction (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">&gt;&gt;&gt; </span><span class="s1">&#39;twisted&#39;</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">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">twisted</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;twisted&#39;</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">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="s1">&#39;twisted&#39;</span><span class="p">]</span>
<span class="go">&lt;module &#39;twisted&#39; from &#39;/usr/lib/python2.7/dist-packages/twisted/__init__.pyc&#39;&gt;</span>
<span class="go">&gt;&gt;&gt;</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> nest pas forcément importé
dans lespace de nommage usuel. Il faut importer le module pour
pouvoir lutiliser.</p>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="s1">&#39;email&#39;</span><span class="p">]</span>
<span class="go">&lt;module &#39;email&#39; from &#39;/usr/lib/python2.7/email/__init__.pyc&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </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">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">NameError</span>: <span class="n">name &#39;email&#39; is not defined</span>
<span class="go">&gt;&gt;&gt;</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">&lt;</span><span class="n">module</span><span class="o">&gt;.</span><span class="vm">__file__</span><span class="p">))</span>
</pre></div>
</div>
<p>Pour importer un module qui nest 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">&#39;/path/to/mymodule/custom.py&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</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">&#39;dhcp_custom&#39;</span><span class="p">,</span> <span class="n">fch</span><span class="p">,</span> <span class="s1">&#39;/path/to/mymodule.py&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;.py&#39;</span><span class="p">,</span> <span class="s1">&#39;U&#39;</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 dun 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>Cest suivant la version de python car cest 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">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sqlalchemy</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqlalchemy</span><span class="o">.</span><span class="n">__version__</span>
<span class="go">&#39;0.9.8&#39;</span>
<span class="go">&gt;&gt;&gt;</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">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">specialmethods</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span> <span class="o">=</span> <span class="n">Zone</span><span class="p">(</span><span class="s2">&quot;titi&quot;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z2</span> <span class="o">=</span> <span class="n">Zone</span><span class="p">(</span><span class="s2">&quot;tutu&quot;</span><span class="p">,</span> <span class="mi">40</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span> <span class="o">&gt;</span> <span class="n">z2</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span> <span class="o">+</span> <span class="n">z2</span>
<span class="go">&lt;specialmethods.Zone object at 0x7f02d95fb190&gt;</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">z3</span><span class="o">.</span><span class="n">name</span>
<span class="go">&#39;tititutu&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z3</span><span class="o">.</span><span class="n">level</span>
<span class="go">50</span>
<span class="go">&gt;&gt;&gt;</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, cest-à-dire que le protocole daccès
aux attributs est règlable.</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">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">&quot;a class attribute&quot;</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cobj</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cobj</span><span class="o">.</span><span class="n">classattr</span>
<span class="go">&#39;a class attribute&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cobj</span><span class="o">.</span><span class="n">insattr</span> <span class="o">=</span> <span class="s2">&quot;an instance attribute&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cobj</span><span class="o">.</span><span class="n">insattr</span>
<span class="go">&#39;an instance attribute&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;classattr&#39;</span><span class="p">]</span>
<span class="go">&#39;a class attribute&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cobj</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;insattr&#39;</span><span class="p">]</span>
<span class="go">&#39;an instance attribute&#39;</span>
</pre></div>
</div>
<p>les attributs ne sont pas systématiquement encapsulées en python.</p>
<p>pour contrôler laccè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">&#39;src&#39;</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">&quot;method&quot;</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">&quot;method&quot;</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 dune classe</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">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">&quot;a&quot;</span><span class="p">,</span><span class="s2">&quot;b&quot;</span><span class="p">,</span><span class="s2">&quot;c&quot;</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">Bar</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="s2">&quot;toto&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">a</span>
<span class="go">&#39;toto&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">d</span> <span class="o">=</span> <span class="s2">&quot;titi&quot;</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">?</span>
<span class="gr">AttributeError</span>: <span class="n">&#39;Bar&#39; object has no attribute &#39;d&#39;</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">lencapsulation nest pas une condition de base de la programmation
par objects, surtout que le contrôle nuit à lagilité.</p>
</div>
<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">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">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">AttributeError</span>: <span class="n">&#39;Point&#39; object has no attribute &#39;z&#39;</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<ul class="simple">
<li>notation <cite>|</cite> et notation <cite>&gt;</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">&lt;&lt;</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> | &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="exceptions.html" title="Le style de programmation par exceptions"
>suivant</a> |</li>
<li class="right" >
<a href="prompt.html" title="Interactions avec lutilisateur"
>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>