hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Holy Red Snakes! (Cont'd.) #

by why in inspect

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.

said on 03 Jun 2005 at 13:43

Well, as a Ruby programmer, I certainly like it, since it is quite close to the Ruby version:


def redirect_stdout(new_stdout)
  begin
    save_stdout = $stdout
    $stdout = new_stdout
    yield
  ensure
    $stdout = save_stdout
  end
end

File.open(filename, 'w') do |file|
  redirect_stdout(file) do
    puts 'Hello world'
  end
end

I don’t see how Pythoneers have lived so long without it.

said on 03 Jun 2005 at 14:19

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???).

said on 03 Jun 2005 at 15:09

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.

said on 03 Jun 2005 at 15:33

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.

said on 03 Jun 2005 at 16:31

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.

said on 03 Jun 2005 at 17:04

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”

said on 04 Jun 2005 at 00:00
It’s grrrreaattt!! Finally Python will have Ruby’s anonymous code blocks! It’s icky. Nowhere near what Ruby has, but don’t say that too loud. We’d really like the Pythonistas to adopt something like this while thinking that they will now get anonymous code blocks like Ruby. It’ll make them feel better, anyway. The example shown above shows only one statement in the ‘code block’ – is that imposed by Python’s crippled lambdas which only allow one statement?
said on 05 Jun 2005 at 05:31

I think I prefer the name ‘PyBy

Comments are closed for this entry.