A friendlier, sugarier lambda -- a proposal for Ruby-like blocks in python

brenocon at gmail.com brenocon at gmail.com
Sat Oct 14 17:56:09 EDT 2006


Kay Schluehr wrote:
> The with statement is already implemented in Python 2.5.
>
> http://docs.python.org/whatsnew/pep-343.html
>
> The main difference between the with statement and Ruby blocks is that
> the with-statement does not support loops. Yielding a value of a
> function decorated with a contextmanager and passing it to the BLOCK of
> the with statement is essentially a one-shot. Therefore you can't use
> the with statement to define iterators. It is not a lightweight visitor
> pattern replacement as it is in Ruby. Hence the with- and the
> for-statement are orthogonal to each other in Python.

Thanks or the What's-New link, it clarified things for me.  So there
are several ways to do things with code blocks now in python..
 * for/while define loops around their blocks
 * if defines contional control into its block
 * with defines startup/cleanup context surrounding its block

Twisted addCallback() is a different pattern than either of these.  The
code is deferred to execute at some later time.  If there are many more
patterns of things you could want to do with a block, it might be nice
to have a blocks-are-closures mechanism.




More information about the Python-list mailing list