[Python-Dev] Ruby-style Blocks in Python [Pseudo-PEP]

Matthew Wilkes matthew at matthewwilkes.co.uk
Sun Mar 8 17:02:33 CET 2009

On 8 Mar 2009, at 15:13, tav wrote:

> Apologies for bringing up an old issue, but I think I've worked out a
> Pythonic syntax for doing Ruby-style blocks. The short of it is:
>    with employees.select do (emp):
>        if emp.salary > developer.salary:
>            return fireEmployee(emp)
>        else:
>            return extendContract(emp)

My train of thought when seeing this:

  1. Ok, "with" it's a context manager.
  2. Huh, no it's not, "do", it's a loop.
  3. What on earth is emp?  Where's that defined?  Why the parens?
  4. Where do those returns return to?
  5. I have no idea what this does.

Ah, now, reading your Python alternative, defining a function then  
passing it is, it's more clear.

This is completely incomprehensible to me, it doesn't feel like  
python.  I'd rather define the throwaway function, anonymous callables  
that do complex things aren't my kind of thing.  Give them a sensible  
name, put them in a utils module, and import it where needed, it's  
much clearer.


More information about the Python-Dev mailing list