This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: Holy Red Snakes! (Cont'd.)
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
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.