在python中使用contextlib

时间:2015-05-10 16:21:27

标签: python

我一直在尝试将contextlib与Python一起使用...但它返回错误...代码是

from contextlib import contextmanager
import contextlib
class Mgr():
     def __enter__(self): 
         return self
     def __exit__(self, ext, exv, trb):
         if ext is not None: print "no not possible"
            print "OK I caught you"
            return True
     @contextlib.contextmanager
     def honey(self):
         print 'guli'

with contextmanager() as d:
     print d 

当我运行此代码时,它有像我这样的错误

TypeError: contextmanager() takes exactly 1 argument (0 given)

代码在没有contextlib的情况下正常工作。但我需要使用contextlib ..

如何使用honey调用contextmanager函数?..

0 个答案:

没有答案