News for hackers. Stuff that matters.
Re:Python Best Practice Patterns by ajtulloch on Friday February 28, 2014 @12:02PM (#7319950)
For simple context managers, an easier method is to use contextlib.contextmanager (http://docs.python.org/library/contextlib.html).

  from contextlib import contextmanager
  
  @contextmanager
  def tag(name):
      print "<%s>" % name
      yield
      print "</%s>" % name
  
  with tag("h1"):
      print "foo"
  """
  <h1>
  foo
  </h1>
  """

There are no comments on this comment yet.