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

Carl Johnson cmjohnson.mailinglist at gmail.com
Tue Mar 10 06:59:39 CET 2009


> Sounds like you might as well write a decorator named @using:
>
>  @using(employees.select)
>  def _(employee):
>     if employee.salary > developer.salary:
>         fireEmployee(employee)
>     else:
>         extendContract(employee)

I like that (which is why I proposed allowing lambda decorators last
month), but I'm uncomfortable with how after all is said and done, _
will either be set to something that's not a callable or to a callable
that no one is ever supposed to call. Perhaps if we allowed for this:

@using(employees.select) as results
def (employee): #It is mandatory that no name be used here and
               #that parentheses are included
  if employee.salary > developer.salary:
      fireEmployee(employee)
  else:
      extendContract(employee)

#results = [ list of employees ]

to be syntatic sugar for this:

def callback(employee):
  if employee.salary > developer.salary:
      fireEmployee(employee)
  else:
      extendContract(employee)

results = using(employees.select)(callback)
#results = [ list of employees ]

Similarly, the horrible Java-style callback mentioned earlier in the thread

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

might with a change in API become something like

@Bind(wx.BUTTON, mybutton) as connected_button_object
def (event):
   #do event handling stuff…
   return

Ruby blocks let them write,

>> (1..10).map { |x| 2 * x }
=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

which for us would be a simple [2 * x for x in range(1, 11)], but
their version has the advantage of being able to be expanded to a
series of expressions and statements instead of a single expression if
need be. With my proposal, someone could write:

>>> def map_dec(l):
...     def f(callback):
...         return [callback(item) for item in l]
...     return f
...
>>> @map_dec(range(10)) as results
... def (item):
...     return 2 * item
...
>>> results
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Basically, the "as" would be used to indicate "no one cares about the
following function by itself, they just want to use it as a callback
to get some result".

My-doomed-proposally-yours,

-- Carl



More information about the Python-ideas mailing list