[issue9110] contextlib.ContextDecorator

Jack Diederich report at bugs.python.org
Sat Jul 10 05:39:26 CEST 2010

Jack Diederich <jackdied at gmail.com> added the comment:


Short version: This isn't theoretical because I use context managers and function decorators interchangeably and constantly.

Long Version: Function decorators and context managers have very similar use cases.  They both go something like:
  1) add optional extra state
  2) execute the original function (decorator) or block (context manager)
  3) add optional extra exception handling or do something special based on the extra state.

Frood's mock library does this in a very sane way.  ex/
@mock.patch(sys, 'stdio', someStringIOInstance)
def test_blah(self): pass

# this test uses context instead of decorators
def test_blaise(self):
  # test setup here
  with @mock.patch(sys, 'stdin', someStringIOInstance):
    dummy = 'something particular to this setup'
  # more tests here

So the use isn't theoretical [at a minimum he's doing it and I'm doing it], now we're just talking about what is the most obvious interface.


Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list