<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" > <channel> <title>Sam & Max » print</title> <atom:link href="http://sametmax.com/tag/print/feed/" rel="self" type="application/rss+xml" /> <link>http://sametmax.com</link> <description>Du code, du cul</description> <lastBuildDate>Sat, 07 Nov 2015 10:56:13 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=4.1</generator> <item> <title>Conséquences de print devenant une fonction en Python 3 13</title> <link>http://sametmax.com/consequence-de-print-devenant-une-fonction-en-python-3/</link> <comments>http://sametmax.com/consequence-de-print-devenant-une-fonction-en-python-3/#comments</comments> <pubDate>Tue, 03 Dec 2013 08:31:37 +0000</pubDate> <dc:creator><![CDATA[Sam]]></dc:creator> <category><![CDATA[Programmation]]></category> <category><![CDATA[print]]></category> <category><![CDATA[python]]></category> <category><![CDATA[python 3]]></category> <guid isPermaLink="false">http://sametmax.com/?p=8164</guid> <description><![CDATA[Devoir utiliser <code>print()</code> au lieu de <code>print</code> m'arrache la gueule, je dois l'avouer. J'ai l'impression que ces deux parenthèses ma prennent 5 heures à taper, là où avant mon petit espace était à portée de pouce. Mais évidement, la décision de faire de <code>print</code> une fonction est parfaitement rationnelle, et en voici tous les avantages...]]></description> <content:encoded><![CDATA[<p>Devoir utiliser <code>print()</code> au lieu de <code>print</code> m’arrache la gueule, je dois l’avouer. J’ai l’impression que ces deux parenthèses ma prennent 5 heures à taper, là où avant mon petit espace était à portée de pouce.</p> <p>Mais évidement, la décision de faire de <code>print</code> une fonction est parfaitement rationnelle, et en voici tous les avantages…</p> <h2><code>print</code> n’est plus une déclaration, mais une expression</h2> <p>Il y a deux types d’instructions en Python : les déclarations (‘statement’ dans la langue de Chuck Norris) et les expressions.</p> <p>Les déclarations sont des instructions indépendantes : <code>while</code>, <code>var = 'valeur'</code>, <code>try</code>, <code>def</code>, etc. On ne peut pas les mettre dans une expression, seulement les imbriquer dans une autre déclaration.</p> <p>Les expressions, elles, sont imbricables dans n’importe quoi, et elles retournent toujours quelques chose quelque chose, fusse <code>None</code>. Parmis les expressions on retrouve : les lambdas, les listes en intentions, les appels de fonctions, etc.</p> <p>Avant, <code>print</code> était une déclaration, ce qui était très peu souple. Maintenant, c’est un appel de fonction, et donc une expression.</p> <p>On peut donc utiliser <code>print</code> dans une autre expression et ainsi :</p> <p>Dans une <a href="http://sametmax.com/fonctions-anonymes-en-python-ou-lambda/">lambda</a> :</p> <div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;">crier <span style="color: #66cc66;">=</span> <span style="color: #ff7700;font-weight:bold;">lambda</span> x<span style="color: #66cc66;">,</span> *a<span style="color: #66cc66;">,</span> **k: <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">(</span>x.<span style="color: black;">upper</span><span style="color: black;">(</span><span style="color: black;">)</span><span style="color: #66cc66;">,</span> *a<span style="color: #66cc66;">,</span> **k<span style="color: black;">)</span></pre></td></tr></table></div> <p>Dans une <a href="http://sametmax.com/python-love-les-listes-en-intention-partie/">liste en intention</a> :</p> <div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">[</span><span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">(</span>x<span style="color: black;">)</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">(</span><span style="color: #ff4500;">10</span><span style="color: black;">)</span><span style="color: black;">]</span></pre></td></tr></table></div> <h2>Plus de syntaxe bizarre</h2> <p>Comment supprimer le saut de ligne quand on print avec l’ancienne syntaxe ?</p> <div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"Hello"</span><span style="color: #66cc66;">,</span> <span style="color: #808080; font-style: italic;"># <- notez la virgule</span> <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"You"</span> Hello You</pre></td></tr></table></div> <p>Avec la nouvelle syntaxe :</p> <div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">(</span><span style="color: #483d8b;">"Hello "</span><span style="color: #66cc66;">,</span> end<span style="color: #66cc66;">=</span><span style="color: #483d8b;">''</span><span style="color: black;">)</span> <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">(</span><span style="color: #483d8b;">'You'</span><span style="color: black;">)</span></pre></td></tr></table></div> <p>Comment rediriger le print vers stderr avec l’ancienne syntaxe ?</p> <div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">>>></span> <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #66cc66;">>></span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'arg !'</span></pre></td></tr></table></div> <p>Avec la nouvelle ?</p> <div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">>>></span> <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">(</span><span style="color: #483d8b;">'arg !'</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">file</span><span style="color: #66cc66;">=</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span><span style="color: black;">)</span></pre></td></tr></table></div> <p>Comment faire une liste séparée par des virgules avec l’ancienne syntaxe ?</p> <div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">>>></span> l <span style="color: #66cc66;">=</span> <span style="color: black;">[</span><span style="color: #483d8b;">'Des petits trous'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'des petits trous'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'toujours des petits trous'</span><span style="color: black;">]</span> <span style="color: #66cc66;">>>></span> <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">', '</span>.<span style="color: black;">join</span><span style="color: black;">(</span>l<span style="color: black;">)</span> Des petits trous<span style="color: #66cc66;">,</span> des petits trous<span style="color: #66cc66;">,</span> toujours des petits trous</pre></td></tr></table></div> <p>Avec la nouvelle ?</p> <div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">>>></span> <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">(</span>*l<span style="color: #66cc66;">,</span> sep<span style="color: #66cc66;">=</span><span style="color: #483d8b;">', '</span><span style="color: black;">)</span></pre></td></tr></table></div> <p>En gros, la syntaxe est unifiée, plus besoin de penser à tous ces cas particuliers. Et en plus on peut demander de l’aide avec <code>help(print)</code> alors qu’avant ça faisait une syntaxe error.</p> <h2>On peut récupérer une référence sur <pre>print</pre> </h2> <p>Et donc la passer en paramètre pour faire de l’<a href="http://sametmax.com/quest-ce-quun-callback/">injection de dépendance</a> :</p> <div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> truc<span style="color: black;">(</span>a<span style="color: #66cc66;">,</span> <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: #66cc66;">=</span><span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">)</span>: <span style="color: #808080; font-style: italic;"># faire un truc avec a</span> <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">(</span>a<span style="color: black;">)</span><span style="color: #66cc66;">;</span> <span style="color: #808080; font-style: italic;"># on peut utiliser print normalement</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span> log <span style="color: #66cc66;">=</span> loging.<span style="color: black;">getLogger</span><span style="color: black;">(</span><span style="color: black;">)</span> truc<span style="color: black;">(</span>machin<span style="color: #66cc66;">,</span> <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: #66cc66;">=</span>log<span style="color: black;">)</span> <span style="color: #808080; font-style: italic;"># on print pas, on log !</span> truc<span style="color: black;">(</span>autre_machin<span style="color: #66cc66;">,</span> <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: #66cc66;">=</span><span style="color: #ff7700;font-weight:bold;">lambda</span> *a<span style="color: #66cc66;">,</span> **k: <span style="color: #008000;">None</span><span style="color: black;">)</span> <span style="color: #808080; font-style: italic;"># ignore les prints !</span></pre></td></tr></table></div> <p>Et également appliquer tous les outils de programmation fonctionnelle de Python :</p> <div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span> <span style="color: #ff7700;font-weight:bold;">from</span> functools <span style="color: #ff7700;font-weight:bold;">import</span> partial error <span style="color: #66cc66;">=</span> partial<span style="color: black;">(</span><span style="color: #ff7700;font-weight:bold;">print</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">file</span><span style="color: #66cc66;">=</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span><span style="color: black;">)</span> error<span style="color: black;">(</span><span style="color: #483d8b;">'Wrong !'</span><span style="color: black;">)</span> <span style="color: #808080; font-style: italic;"># va directement sur stderr</span></pre></td></tr></table></div> <h2>Activer <code>print()</code> en Python 2.7</h2> <p>Si vous voulez prendre toute de suite de bonnes habitudes, vous pouvez faire, en Python 2.7 :</p> <div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">__future__</span> <span style="color: #ff7700;font-weight:bold;">import</span> print_function</pre></td></tr></table></div> ]]></content:encoded> <wfw:commentRss>http://sametmax.com/consequence-de-print-devenant-une-fonction-en-python-3/feed/</wfw:commentRss> <slash:comments>13</slash:comments> </item> </channel> </rss>