[Python-ideas] Ruby-style Blocks in Python Idea

average dreamingforward at gmail.com
Mon Mar 9 17:43:03 CET 2009


While there are several complaining that all this can be done with a
def, there's an critical distinction being overlooked.  Passing around
code blocks is a very different style of programming that Python or
most languages like it have ever experimented with.  The programming
art itself hasn't really even explored the different range of thinking
this style of programming opens up.  Where as most function
definitions are verb-like, this would be a noun-like definition.  Akin
perhaps to the difference between a hormone in the body and a
neurotransmitter, respectively.Setting it off with a new keyword, or
expanding the use of lambda is really a mandatory way of signifying
this INTENT.

marcos

> I've come up with a way to do Ruby-style blocks in what I feel to be a
> Pythonic way:
>
>  using employees.select do (employee):
>      if employee.salary > developer.salary:
>          fireEmployee(employee)
>      else:
>          extendContract(employee)
>
> I originally overloaded the ``with`` keyword, but on Guido's guidance
> and responses from many others, switched to the ``using`` keyword.
>
> It covers everything from why these are useful to a proposal of how
> the new ``do`` statement and __do__ function could work.



More information about the Python-ideas mailing list