<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr">
	<id>https://wiki.squi.fr/index.php?action=history&amp;feed=atom&amp;title=Python_%3A_with_et_les_gestionnaires_de_contexte</id>
	<title>Python : with et les gestionnaires de contexte - Historique des versions</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.squi.fr/index.php?action=history&amp;feed=atom&amp;title=Python_%3A_with_et_les_gestionnaires_de_contexte"/>
	<link rel="alternate" type="text/html" href="https://wiki.squi.fr/index.php?title=Python_:_with_et_les_gestionnaires_de_contexte&amp;action=history"/>
	<updated>2026-04-28T00:27:19Z</updated>
	<subtitle>Historique des versions pour cette page sur le wiki</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>https://wiki.squi.fr/index.php?title=Python_:_with_et_les_gestionnaires_de_contexte&amp;diff=2221&amp;oldid=prev</id>
		<title>Justine le 22 juin 2022 à 12:04</title>
		<link rel="alternate" type="text/html" href="https://wiki.squi.fr/index.php?title=Python_:_with_et_les_gestionnaires_de_contexte&amp;diff=2221&amp;oldid=prev"/>
		<updated>2022-06-22T12:04:30Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;fr&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Version précédente&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Version du 22 juin 2022 à 12:04&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Ligne 1 :&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Ligne 1 :&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:prog]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:python]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Les informations sont issues de : [http://sametmax.com/les-context-managers-et-le-mot-cle-with-en-python/ http://sametmax.com/les-context-managers-et-le-mot-cle-with-en-python/]  &lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Les informations sont issues de : [http://sametmax.com/les-context-managers-et-le-mot-cle-with-en-python/ http://sametmax.com/les-context-managers-et-le-mot-cle-with-en-python/]  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;= Sans context manager =&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;= Sans context manager =&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key mediawiki-mediawiki:diff:1.41:old-325:rev-2221:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Justine</name></author>
	</entry>
	<entry>
		<id>https://wiki.squi.fr/index.php?title=Python_:_with_et_les_gestionnaires_de_contexte&amp;diff=325&amp;oldid=prev</id>
		<title>Justine : Justine a déplacé la page Python : Context Manager vers Python : with et les gestionnaires de contexte : PASSKE G ENVI</title>
		<link rel="alternate" type="text/html" href="https://wiki.squi.fr/index.php?title=Python_:_with_et_les_gestionnaires_de_contexte&amp;diff=325&amp;oldid=prev"/>
		<updated>2018-11-25T21:29:03Z</updated>

		<summary type="html">&lt;p&gt;Justine a déplacé la page &lt;a href=&quot;/index.php/Python_:_Context_Manager&quot; class=&quot;mw-redirect&quot; title=&quot;Python : Context Manager&quot;&gt;Python : Context Manager&lt;/a&gt; vers &lt;a href=&quot;/index.php/Python_:_with_et_les_gestionnaires_de_contexte&quot; title=&quot;Python : with et les gestionnaires de contexte&quot;&gt;Python : with et les gestionnaires de contexte&lt;/a&gt; : PASSKE G ENVI&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;fr&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Version précédente&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Version du 25 novembre 2018 à 21:29&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;fr&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(Aucune différence)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key mediawiki-mediawiki:diff:1.41:old-324:rev-325 --&gt;
&lt;/table&gt;</summary>
		<author><name>Justine</name></author>
	</entry>
	<entry>
		<id>https://wiki.squi.fr/index.php?title=Python_:_with_et_les_gestionnaires_de_contexte&amp;diff=324&amp;oldid=prev</id>
		<title>Justine : Page créée avec « Les informations sont issues de : [http://sametmax.com/les-context-managers-et-le-mot-cle-with-en-python/ http://sametmax.com/les-context-managers-et-le-mot-cle-with-en-py... »</title>
		<link rel="alternate" type="text/html" href="https://wiki.squi.fr/index.php?title=Python_:_with_et_les_gestionnaires_de_contexte&amp;diff=324&amp;oldid=prev"/>
		<updated>2018-11-25T21:28:21Z</updated>

		<summary type="html">&lt;p&gt;Page créée avec « Les informations sont issues de : [http://sametmax.com/les-context-managers-et-le-mot-cle-with-en-python/ http://sametmax.com/les-context-managers-et-le-mot-cle-with-en-py... »&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Nouvelle page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Les informations sont issues de : [http://sametmax.com/les-context-managers-et-le-mot-cle-with-en-python/ http://sametmax.com/les-context-managers-et-le-mot-cle-with-en-python/] &lt;br /&gt;
= Sans context manager =&lt;br /&gt;
&lt;br /&gt;
Les context manager passent par le mot-clef with. Ce mot-clef agit un peu comme un décorateur : il permet d&amp;#039;exécuter du code automatiquement avant et après un autre code. De la même façon qu&amp;#039;avec les décorateurs, on pourrait faire tout ça sans utiliser le mot-clef with, il s&amp;#039;agit surtout d&amp;#039;une question de praticité (Sam parle même d&amp;quot;une question de style&amp;quot;!).&lt;br /&gt;
&lt;br /&gt;
Je vais reprendre l&amp;#039;exemple de Sam :&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background: rgb(238, 238, 238) none repeat scroll 0% 0%;  border: 1px solid rgb(204, 204, 204);  padding: 5px 10px&amp;quot;&amp;gt;def truc():&lt;br /&gt;
    print &amp;quot;machin&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
print &amp;quot;Avant&amp;quot;&lt;br /&gt;
try:&lt;br /&gt;
    truc()&lt;br /&gt;
finally:&lt;br /&gt;
    print &amp;quot;Après&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ici, même si je fais en sorte que ma fonction truc lève une exception, &amp;quot;Après&amp;quot; sera quand même affiché, malgré le plantage.&lt;br /&gt;
&lt;br /&gt;
= Avec les context managers =&lt;br /&gt;
&lt;br /&gt;
Un context manager est une classe ordinaire, sa spécifité est l&amp;#039;utilisation d&amp;#039;une méthode __enter__() et d&amp;#039;une méthode __exit__(); ce sont des méthodes normales, leur nom est juste une convention (qu&amp;#039;il convient de respecter!).&lt;br /&gt;
&amp;lt;div style=&amp;quot;background:#eeeeee; border:1px solid #cccccc; padding:5px 10px&amp;quot;&amp;gt;&amp;lt;pre&amp;gt;class monContextManager(object):&lt;br /&gt;
    &lt;br /&gt;
    def __enter__(self):&lt;br /&gt;
        print(&amp;quot;Avant&amp;quot;)&lt;br /&gt;
    &lt;br /&gt;
    def __exit__(self, type, value, traceback):&lt;br /&gt;
        print(&amp;quot;Après&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
def truc():&lt;br /&gt;
    print(&amp;quot;Coucou !&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
with monContextManager:&lt;br /&gt;
    truc()&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;/div&amp;gt; &lt;br /&gt;
&amp;amp;nbsp;Pour une raison que j&amp;#039;ignore, cela ne fonctionne pas, mais c&amp;#039;est juste pour illustrer : le mot-clef with sert à englober des instructions dans un gestionnaire de contexte. C&amp;#039;est plus ou moins la même chose qu&amp;#039;un try/finally, en plus lisible. Je prend un morceau de code, je l&amp;#039;imbrique au milieu et j&amp;#039;exécute le tout.&lt;/div&gt;</summary>
		<author><name>Justine</name></author>
	</entry>
</feed>