<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Times New Roman",serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=FR-CA link=blue vlink=purple><div class=WordSection1><div><div><div><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-CA>On Mon, Mar 28, 2016 at 8:54 AM, Émanuel Barry <</span><a href="mailto:vgr255@live.ca" target="_blank"><span lang=EN-CA>vgr255@live.ca</span></a><span lang=EN-CA>> wrote:<o:p></o:p></span></p><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm'><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:35.4pt'><span lang=EN-CA style='font-size:11.0pt;font-family:Consolas;color:#1F497D'>I wonder how reasonable it would be to add a new keyword to open that would .close() the file object upon a single write/read. Consider:</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:35.4pt'><span lang=EN-CA style='font-size:11.0pt;font-family:Consolas;color:#1F497D'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:35.4pt'><span lang=EN-CA style='font-size:11.0pt;font-family:Consolas;color:#1F497D'>data = open("foo.txt", "r", close_after_use=True).read()</span><o:p></o:p></p></div></div></blockquote><div><p class=MsoNormal style='margin-left:35.4pt'><o:p> </o:p></p></div><div><p class=MsoNormal style='margin-left:35.4pt'>it makes it a tiny bit shorter than  using "with", but doesn't solve Nick's mental model issue -- the user still needs to be thinking about the fact that they are creating a file object and that it needs to be closed when you are done with it.<o:p></o:p></p></div><div><p class=MsoNormal style='margin-left:35.4pt'><o:p> </o:p></p></div><div><p class=MsoNormal style='margin-left:35.4pt'>and I"m not sure how you would define "use" -- any i.o. opeartion? i.e.:<o:p></o:p></p></div><div><p class=MsoNormal style='margin-left:35.4pt'><o:p> </o:p></p></div><p class=MsoNormal style='margin-left:35.4pt'><span style='font-family:"Courier New"'>infile = open("foo.txt", "r", close_after_use=True)<br>first_line = infile.readline()</span><o:p></o:p></p></div><div><p class=MsoNormal style='margin-left:35.4pt'><o:p> </o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.0pt;margin-left:35.4pt'><span style='font-family:"Courier New"'>now what is the state of infile? a closed file object?</span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-CA style='color:#1F497D'>Sure. The keyword really could benefit from a better name, but it might solve some part of the issue. It doesn’t solve the mental model issue though, but to be fair, </span><span lang=EN-CA style='font-family:Consolas;color:#1F497D'>open()</span><span lang=EN-CA style='color:#1F497D'> should always be wrapped in a context manager (IMO, anyway).<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-CA style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-CA style='color:#1F497D'>-Emanuel<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-CA style='font-size:11.0pt;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-CA>exactly why context managers were introduced:<o:p></o:p></span></p></div></div><div><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-CA style='font-family:"Courier New"'><o:p> </o:p></span></p></div><p class=MsoNormal style='margin-left:35.4pt'><span lang=EN-CA style='font-family:"Courier New"'>w</span><span style='font-family:"Courier New"'>ith open("foo.txt", "r", close_after_use=True) as infile:<br>    first_line = infile.readline()<br><br>something_else...</span><o:p></o:p></p><div><p class=MsoNormal style='margin-left:35.4pt'><o:p> </o:p></p></div><div><p class=MsoNormal style='margin-left:35.4pt'>now it's pretty clear that <span style='font-family:"Courier New"'>infile</span> is no longer a useful object.<o:p></o:p></p></div><div><p class=MsoNormal style='margin-left:35.4pt'><o:p> </o:p></p></div><div><p class=MsoNormal style='margin-left:35.4pt'>-CHB<o:p></o:p></p></div><div><div><p class=MsoNormal><o:p> </o:p></p></div></div></div></div></body></html>