-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpythons-with.html
92 lines (77 loc) · 5.98 KB
/
pythons-with.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta id="keywords" name="keywords" content="">
<link id="stylesheet" rel="stylesheet" type="text/css" href="https://oskipa.github.io/theme/css/code.css"></link>
<link id="stylesheet" rel="stylesheet" type="text/css" href="https://oskipa.github.io/theme/css/site.css"></link>
</head>
<body>
<header class="header">
<h1 class="site-name">
<a class="home-link" href="https://oskipa.github.io">Dada</a><br />
|> Structures<br />
|> Algorithms</h1>
<nav>
<menu class="minimal-menu">
<li><a href="https://oskipa.github.io/pages/today-i-learned.html">til</a></li>
<li><a href="https://oskipa.github.io/pages/now.html">now</a></li>
<li><a href="https://oskipa.github.io/short_index.html">index</a></li>
<!--
<li><a href="category/log.html">web log<a></li>
<li><a href="">about<a></li>
<li><a href="">site map<a></li>
-->
</menu>
</nav>
</header>
<div class="container">
<div class="row">
<div class="col-md-8">
<h2>Python's with</h2>
<h3>A small tutorial for a handy feature</h3>
<label>28 July, 2020</label>
<h3>The point</h3>
<p>The "with" statement can be thought as syntactical sugar that replaces a try: except: finally: block with a cleaner syntax.</p>
<p>Let's focus on the practical application of with. This is an example taken from the PEP. You will see how clear the code is. What you see is your actual code rather than ceremonial templates.</p>
<div class="highlight"><pre><span></span><code><span class="w"> </span><span class="nv">with</span><span class="w"> </span><span class="nv">opened</span><span class="ss">(</span><span class="s2">"/etc/passwd"</span><span class="ss">)</span><span class="w"> </span><span class="nv">as</span><span class="w"> </span><span class="nv">f</span>:
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nv">line</span><span class="w"> </span><span class="nv">in</span><span class="w"> </span><span class="nv">f</span>:
<span class="w"> </span><span class="nv">print</span><span class="w"> </span><span class="nv">line</span>.<span class="nv">rstrip</span><span class="ss">()</span>
</code></pre></div>
<p>This would be equivalent to something like</p>
<div class="highlight"><pre><span></span><code> <span class="kn">import</span> <span class="nn">sys</span>
<span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"/etc/passwd"</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">except</span><span class="p">:</span>
<span class="n">e</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">raise</span> <span class="n">e</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</code></pre></div>
<p>As you can see, using "with" is cleaner.</p>
<p>The one catch is that the object must fit some conditions for it to work with the "with" statement. File objects since Python 2.5 meet them. You can also create objects that can play nicely along "with."</p>
<h3>The long story</h3>
<p>A friend told me that I should look into Python's "with" because he said that it trips some people new to the language. I found this document doing an online search <a href="https://www.python.org/dev/peps/pep-0343/">PEP 343, the with statement</a>[1]. This document goes into detail on the evolution of the feature and how to use it. If you have time and you are interested, you should read this document.</p>
<p>As a side note, the Python Enhancement Proposal (PEP), seems to be a great documentation project. I need to think about this and see how workplaces can adopt something like this to document design decisions over time.</p>
<p>Back to with! In practical terms, "with" is some sort of syntactical sugar that allows you to avoid writing template try: except: finally: blocks of code. It seems similar to the "using" statement in c#. For us to be able to use a "with" statement, the object needs to support __enter__() and __exit__() methods. The PEP quoted above goes into detail on the requirements for "with" to work.</p>
<p>If you find the PEP document too overwhelming, as I did, you can also look at the following blog entry by Fredrik Lundh, <a href="https://effbot.org/zone/python-with-statement.htm">Understanding Python's "with" statement</a>[2].</p>
<p>References:</p>
<p>[1] <a href="https://www.python.org/dev/peps/pep-0343/">PEP 343, the with statement</a></p>
<p>[2] Lundh, Fredrik <a href="https://effbot.org/zone/python-with-statement.htm">Understanding Python's "with" statement</a></p>
</div>
</div>
</div>
<footer class="footer">
<!-- <p class="about"><a href="http://hugoestr.github.io/blog/manifesto.html">About</a> --> <a href="https://oskipa.github.io/pages/about.html" >I am</a> a software developer. I code in Python, Ruby, Lisp, and many other languages. I like poetry, museums, and ukuleles. I try to guide my life on principles of dadaism and nonviolence.</p>
<p>
<a href="feeds/all.atom.xml">atom</a>
<!-- <a href="http://github.com/oskipa/">github</a> -->
</p>
<p>Artisanally built with <a href="https://blog.getpelican.com/">Pelican</a>. Content handcrafted in <a href="https://www.vim.org/" >Vim</a>. All hallucinations are human. 100% AI Free.</p>
</footer>
</body>
</html>