Ruby-style Blocks in Python [Pseudo-PEP]
Hey all, 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) I explain in detail in this blog article: http://tav.espians.com/ruby-style-blocks-in-python.html It covers everything from why these are useful to a proposal of how the new ``do`` statement and __do__ function could work. Let me know what you think. My apologies if I've missed something obvious. Thanks! -- love, tav plex:espians/tav | tav@espians.com | +44 (0) 7809 569 369 http://tav.espians.com | http://twitter.com/tav | skype:tavespian
tav <tav@espians.com> wrote:
I explain in detail in this blog article:
"This is also possible in Python but at the needless cost of naming and defining a function first" The cost of defining the function first is probably much less than the cost of your __do__ function. Your proposal seems to be much more limited than passing functions around e.g. Python allows you to pass in multiple functions where appropriate, or to store them for later calling.
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. Matt
On Sun, Mar 8, 2009 at 8:13 AM, tav <tav@espians.com> wrote:
Apologies for bringing up an old issue, but I think I've worked out a Pythonic syntax for doing Ruby-style blocks.
As a point of order, please move this discussion to python-ideas, where it belongs roughly until the time a PEP might be ready for approval. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (5)
-
Antoine Pitrou -
Duncan Booth -
Guido van Rossum -
Matthew Wilkes -
tav