<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
</head><body bgcolor="#FFFFFF" text="#000000">Sorry, I was definitely
unclear: I understand that isn't their sole purpose, and what role they
play, in both the iteration examples and others, and I don't mean to
"object" to anything, nor was I suggesting context-manager current
implementation was uneeded, etc.<br>
<br>
My point is that, in the examples I listed, the sole purpose of the
context-manager is to provide a context the for loop will execute in. <br>
<br>
<span style="font-family: monospace;">with manager as context: </span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> # No statements here...</span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> for items in context: </span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> # blah</span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> # no statements here...</span><br
style="font-family: monospace;">
<br>
I was suggesting this scenario, wherein the body of a for loop is, in
fact, the block of code that acts upon some resource or set of resource
being managed by a context manager, might be common enough to warrant a
closer look, as the more "pythonic" approach might accept the for loop's
body as the with statement's code block context. <br>
<br>
<span style="font-family: monospace;">with open(file) as input: </span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> # why have input exist here,
if it's not used, other than...</span><br style="font-family:
monospace;">
<span style="font-family: monospace;"> for line in input:</span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> # do something...</span><br>
<span style="font-family: monospace;"> # why have input--the
context--exist outside of me?</span><br style="font-family: monospace;">
<span style="font-family: monospace;"> # why have input exist here?</span><br>
<br>
And finally, why define the outer <span style="font-family: monospace;">with</span>
block, for the sole pupose of containing the inner "<span
style="font-family: monospace;">for in..</span>" loop's block?<br>
<br>
<br>
<br>
<br>
<br>
<br>
<blockquote style="border: 0px none;"
cite="mid:510D0AA2.2060805@pearwood.info" type="cite">
<div style="margin:30px 25px 10px 25px;" class="__pbConvHr"><div
style="display:table;width:100%;border-top:1px solid
#EDEEF0;padding-top:5px"> <div
style="display:table-cell;vertical-align:middle;padding-right:6px;"><img
photoaddress="steve@pearwood.info" photoname="Steven D'Aprano"
src="cid:part1.00080608.00000407@umbrellacode.com"
name="compose-unknown-contact.jpg" height="25px" width="25px"></div> <div
style="display:table-cell;white-space:nowrap;vertical-align:middle;width:100%">
<a moz-do-not-send="true" href="mailto:steve@pearwood.info"
style="color:#737F92
!important;padding-right:6px;font-weight:bold;text-decoration:none
!important;">Steven D'Aprano</a></div> <div
style="display:table-cell;white-space:nowrap;vertical-align:middle;">
<font color="#9FA2A5"><span style="padding-left:6px">February 2, 2013
4:46 AM</span></font></div></div></div>
<div style="color:#888888;margin-left:24px;margin-right:24px;"
__pbrmquotes="true" class="__pbConvBody">On 02/02/13 22:46, Shane Green
wrote:
<br><blockquote type="cite">with open(path) as input:
<br>for line in input:
<br>do(line)
<br>
<br>Using with to create reference to opened file returned by open() so
it could temporarily be assigned to input for the sole purpose of
iterating its contents never sat very well with me.
<br></blockquote>
<br>It's not the *sole* purpose. If all you want it to iterate over the
file, you can do this:
<br>
<br>for line in open(path):
<br> ...
<br>
<br>
<br>and no context manager is created. The context manager is also
responsible for closing the file immediately you exit the block, without
waiting for the caller to manually close it, or the garbage collector
to (eventually) close it. So it is not *solely* for iteration.
<br>
<br>File context managers can also be used for more than just iteration:
<br>
<br>with open(path) as input:
<br> text = input.read()
<br>
<br>
<br>with open(path, 'r+') as output:
<br> output.write('ZZ')
<br>
<br>and so forth.
<br>
<br>
<br><blockquote type="cite">* The context manager returned by open()
exists only to create the
<br>context and return reference "input";
<br>* the context and code block created by the "with" only exists for
<br>inner "for" loop's code block to execute in.
<br></blockquote>
<br>
<br>I don't understand that objection. As I see it, that's a bit like
saying "the len function exists only to get the length of objects". What
did you expect the context manager to exist for if not to do the things
you say?
<br>
<br>What am I missing?
<br>
<br>
<br>
<br>
<br></div>
<div style="margin:30px 25px 10px 25px;" class="__pbConvHr"><div
style="display:table;width:100%;border-top:1px solid
#EDEEF0;padding-top:5px"> <div
style="display:table-cell;vertical-align:middle;padding-right:6px;"><img
photoaddress="shane@umbrellacode.com" photoname="Shane Green"
src="cid:part1.00080608.00000407@umbrellacode.com"
name="compose-unknown-contact.jpg" height="25px" width="25px"></div> <div
style="display:table-cell;white-space:nowrap;vertical-align:middle;width:100%">
<a moz-do-not-send="true" href="mailto:shane@umbrellacode.com"
style="color:#737F92
!important;padding-right:6px;font-weight:bold;text-decoration:none
!important;">Shane Green</a></div> <div
style="display:table-cell;white-space:nowrap;vertical-align:middle;">
<font color="#9FA2A5"><span style="padding-left:6px">February 2, 2013
3:46 AM</span></font></div></div></div>
<div style="color:#888888;margin-left:24px;margin-right:24px;"
__pbrmquotes="true" class="__pbConvBody">
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<span style="font-family: monospace;">with open(path) as input: </span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> for line in input: </span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> do(line)</span><br
style="font-family: monospace;">
<br>
Using <span style="font-family: monospace; font-weight: bold;">with</span>
to create reference to opened file returned by <span
style="font-family: monospace; font-weight: bold;">open()</span> so it
could temporarily be assigned to <span style="font-family: monospace;
font-weight: bold;">input</span> for the sole purpose of iterating its
contents never sat very well with me. <br>
<ul>
<li>The context manager returned by open() exists only to create the
context and return reference "input"; <br>
</li>
<li>the context and code block created by the "with" only exists for
inner "for" loop's code block to execute in. </li>
</ul>
<span>Now, given a generator
function: <br>
<br>
<span style="font-family: monospace;">def iterwith(cm): <br>
with cm as context: <br>
if context is None:<br>
context = cm<br>
for item in context:<br>
yield item<br>
<br>
</span>The previous code can be turned into: <br>
<br>
<span style="font-family: monospace;">for line in
iterwith(open(path)): </span><br style="font-family: monospace;">
<span style="font-family: monospace;"> do(line) </span><br>
<br>
So, questions: <br>
- Is there anything inherently wrong with the idea, or does it
exist?<br>
- Is it a generally useful tool, or are the examples limited to
files?<br>
- Is it possible a more general mechanism could have value, such as:
<br>
<br>
<span style="font-family: monospace;">for line in file with open(path)
as file: </span><br style="font-family: monospace;">
<span style="font-family: monospace;"> do(line)</span><br
style="font-family: monospace;">
<br>
<span></span> </span><br>
<span>The preceding could be leveraged to different effect: <br>
<br>
</span>
<div style="margin-left: 40px;"><span><span style="font-family:
monospace;"><span style="font-family: monospace;">for line in file with
locked(path): </span></span></span><br style="font-family:
monospace;">
<span style="font-family: monospace;"></span><span><span
style="font-family: monospace;">write(path +
".out", line)</span></span><br>
<span><span style="font-family: monospace;"></span></span></div>
<span><span style="font-family: monospace;"></span></span><span><br>
Or, <br>
</span>
<div style="margin-left: 40px; font-family: monospace;"><span> for
line in input with nested(open(path),lock,open(o</span><span><span>path</span></span><span>))
as input,locked,output: </span><br>
<span></span><span> output.write(line)</span><br>
<span></span></div>
<br>
To revisit the original purpose of "with", this seems to cleanly address
a very common scenario wherein: <br>
<br style="font-family: monospace;">
<span style="font-family: monospace;">resource = create_resource()</span><br
style="font-family: monospace;">
<span style="font-family: monospace;">try: </span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> for item in resource: </span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> do_something(resource,
item)</span><br style="font-family: monospace;">
<span style="font-family: monospace;">except: </span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> raise</span><br
style="font-family: monospace;">
<span style="font-family: monospace;">finally:</span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> cleanup()</span><br
style="font-family: monospace;">
<span style="font-family: monospace;"><br>
# Standard with approach...<br>
with create_resource() as resource:<br>
for item in resource:<br>
do_something(</span><span><span style="font-family: monospace;">resource,
item</span></span><span style="font-family: monospace;">)<br>
<br>
# With for loop as context...<br>
for item in resource with create_resource() as resource: </span><br
style="font-family: monospace;">
<span style="font-family: monospace;"> do_something(resource, </span><span><span
style="font-family: monospace;">item</span></span><span
style="font-family: monospace;">)</span><br>
<br>
And, given the translation into statements, maybe even crazy stuff... <br>
<br>
<span style="font-family: monospace;">[line for line in file with
open(path) as file]</span><br>
<span><br>
J/K, I think.<br>
<br>
<br>
<br>
</span>
</div>
</blockquote>
</body></html>