<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
</head><body bgcolor="#FFFFFF" text="#000000">
<blockquote type="cite"><span><pre wrap="">Perhaps a concrete (even if toy or made-up) example might help me 
understand.</pre></span></blockquote>
<br>
Not sure if this example fits Juancarlo's criterion:<br>
<br>
Here is a place where I really craved for blocks and resorted to using a
 context manager + decorators:<br>
<br>
<a class="moz-txt-link-freetext" href="https://github.com/gitbot/gitbot/blob/master/gitbot/lib/s3.py#L140-L169">https://github.com/gitbot/gitbot/blob/master/gitbot/lib/s3.py#L140-L169</a><br>
<br>
The use case is essentially: recursively loop through a folder and push 
to Amazon S3 evaluating <br>
rules for each file / folder.<br>
<br>
<br>
Here is the implementation:<br>
<a class="moz-txt-link-freetext" href="https://github.com/lakshmivyas/fswrap/blob/master/fswrap.py#L317-L439">https://github.com/lakshmivyas/fswrap/blob/master/fswrap.py#L317-L439</a><br>
<br>
<br>
Just removing the need for the decorators would make this pattern <br>
completely acceptable *for me*.<br>
<br>
<br>
Thanks<br>
Lakshmi<br>
<br>
<br>
Steven D'Aprano wrote:
<blockquote cite="mid:20130513091121.GA23554@ando" type="cite">
  <pre wrap="">On Mon, May 13, 2013 at 03:17:15PM +1000, Nick Coghlan wrote:
</pre>
  <blockquote type="cite"><pre wrap="">On Mon, May 13, 2013 at 2:47 PM, Steven D'Aprano <a class="moz-txt-link-rfc2396E" href="mailto:steve@pearwood.info"><steve@pearwood.info></a> wrote:
</pre><blockquote type="cite"><pre wrap="">On 13/05/13 13:58, Juancarlo Añez wrote:

</pre><blockquote type="cite"><pre wrap="">I don't want new syntax (I think I don't).

What I want is to be able to invoke a block of code repeatedly, within a
context, and in a pythonic way.
</pre></blockquote><pre wrap="">Surely that would be:

with context():
    while condition: # or a for loop
        block of code goes here


If you want something different to this, then I think you do want new
syntax. Otherwise, what do you gain beyond what can already be done now?

Or am I missing something?
</pre></blockquote><pre wrap="">Ruby uses anonymous callbacks for things where Python instead uses
dedicated syntax:

Python -> Ruby

decorated function definitions -> callbacks
for loops + iterator protocol -> callbacks
with statements + context management protocol -> callbacks
callbacks -> callbacks (but with much nicer syntax)

Blocks are a *really* nice way of doing callbacks, so nice that Ruby
just doesn't have some of the concepts Python does - it uses callbacks
instead.
</pre></blockquote>
  <pre wrap=""><!---->
I'm obviously still missing something, because I'm aware of Ruby's 
blocks, but I don't quite see how they apply to Juancarlo's *specific* 
use-case, as described above.

Unless Juancarlo's use-case is more general than I understood, it seems 
to me that we don't need blocks, anonymous or otherwise, to "invoke a 
block of code repeatedly, within a context", in a Pythonic way.

Perhaps a concrete (even if toy or made-up) example might help me 
understand. The only thing I can think of is, if I had a bunch of 
similar loops inside the same context, where only the body of the loop 
was different, I might want to factor it out something like this:


the_block = {define a block of code, somehow}

def do_stuff(block):
    with context:
        while condition:
            {execute the block of code}


do_stuff(the_block)
do_stuff(another_block)


but I think that requires new syntax, and Juancarlo specifically says he 
doesn't want new syntax.


</pre>
</blockquote>
</body></html>