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

Kay Schluehr kay.schluehr at gmx.net
Sun Oct 15 09:55:29 EDT 2006


brenocon at gmail.com wrote:
> 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.

That's true. As I mentioned in my response to Paul Boddie I do think it
is a poor solution even in Ruby(!) but I agree that given the situation
we just have with poorly designed application frameworks ( no illusion:
they won't ever go away and I do not pretend to be smarter and doing it
better in any case ) blocks are a quite nice feature for providing ad
hoc solutions. I consider them as somewhat aligned with a
worse-is-better philosophy.

----

I recently created an extended lambda as an example for my EasyExtend
language extension framework for Python. These kind of showcases are,
at least for me, important for getting programming practice within the
system and explore and extend the framework. The two things I cared
about when altering the semantics of lambda were:
1) Enabling multiple expressions separated by ';'
2) Enabling so called "simple statements"

Python does not only distinguish between expressions and statements but
also between simple and compound statements within its grammar
description. A compound statement is typically multiline and contains a
block. A simple statement is something like print, exec, raise or
assert containing no block. So I actually extended lambda to the limit
of an anonymous closure containing no block.

One might take this into consideration and alter the premises. But this
would be up to you, breno. At the moment I do not recommend using EE
because it is under heavy reconstruction but for the not so distant
future ( end of november is my personal deadline for the next release )
I recommend taking a look on it by anyone who aims to walk the RoR path
of a customized domain specific language for Python. For those who are
dilligent there are will be quite a lot of examples. For some it might
be even fun.




More information about the Python-list mailing list