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

Sturla Molden sturla at molden.no
Mon Mar 9 16:24:23 CET 2009


On 3/9/2009 2:04 PM, tav wrote:
> Hey all,
> 
> 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 believe this is just an extension to the lambda keyword. If lambdas 
could define a block, not just a statement, this would e.g. be

employees.select(
    lambda employee:
       if employee.salary > developer.salary:
          fireEmployee(employee)
       else:
          extendContract(employee)
)

or

tmp = lambda employee:
     if employee.salary > developer.salary:
        fireEmployee(employee)
     else:
        extendContract(employee)

employees.select(tmp)


I see no reason for introducing two new keywords to do this, as you are 
really just enhancing the current lambda keyword.

On the other hand, turning blocks into anonymous functions would be very 
useful for functional programming. As such, I like your suggestion.

This also has a great potential for abuse (as in writing unreadable 
code), just consider how anonymous classes are used in Java's GUI 
toolkits. I really don't want to see

self.Bind(wx.BUTTON, lamda: evt
                          <some huge block of code here>
                               , mybutton)

in wxPython code. But Java programmers coming to Pytho would jump to 
this, as they have been brain washed to use anonymous classes for 
everything (no pun intended).


Sturla Molden



More information about the Python-ideas mailing list