Holy Red Snakes! (Cont'd.) #
As previously reported, Python’s Enhancement #340 pitches the addition of anonymous block statements, an effort to give a dash of Ruby to the firmly indented ledges of Python. But let’s not call it Rupy yet. An updated syntax is out in #343 which politely (and exhaustively) asks for a with
keyword, which optionally invokes __enter__()
and __end__()
methods on the borders of the closure.
To give a taste of how this works, here’s an example which uses two blocks to manage temporary redirection of stdout to a file.
@with_template def redirecting_stdout(new_stdout): save_stdout = sys.stdout sys.stdout = new_stdout try: yield None finally: sys.stdout = save_stdout with opening(filename, "w") as f: with redirecting_stdout(f): print "Hello world"
Anyway, discussion is starting to accumulate today on the WithStatement wiki page.
MrCode
Well, as a Ruby programmer, I certainly like it, since it is quite close to the Ruby version:
I don’t see how Pythoneers have lived so long without it.
Odorf
It’s true; it looks horrible at first, but that’s just the Python syntax for you. That example actually translates directly to Ruby as MrCode notes above. I can’t help thinking our blocks are a lot easier to use, but that may be simply because our syntax is more elegant (and doesn’t rely on indentation!!! Have these people never emailed a file???).
lisp bigot
It warms the cockles of my defmacro-loving heart to see all these young’uns with their closures and anonymous functions and whatnot. Yes indeed, my heart-cockles are positively simmering in my chest, a veritable cardiac stew. A bubbling frothing pool, sputtering forth steam and geyser after geyser of sanguineous vapor. *cough*
I’m sticking with Ruby for now, anyway.
Ian Bicking
Underneith it’s actually very different from Ruby. There’s no blocks involved, but instead a particular protocol for calling functions. From the outside it looks similar, but only for a limited number of the places where Ruby uses blocks. PEP 342 starts covering some of the other cases, though again with a very different implementation.
MrCode
I agree the Ruby version is much better syntax. The whitespace delimited blocks is probably one of the top reasons I don’t program in Python.
riffraff
I concur with what Ian said, the older proposal was more rubyish( but not-quite-so). This one is somewhat more focused, on a single concept, while blocks are a catch all. In this sense it is more like the “for” statement than it is to “lambda”
skypuncher
Toast
I think I prefer the name ‘PyBy
Comments are closed for this entry.