说明
contextlib是为了加强with语句,提供上下文机制的模块,它是通过Generator实现的。通过定义类以及写enter和exit来进行上下文管理;
contextlib中有nested和closing,前者用于创建嵌套的上下文,后则用于帮你执行定义好的close函数.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| class WithinContext(object): def __init__(self,context): print "WithinContext.__init__(%s) " %context def do_something(self): print "WithinContext.do_something()" def __del__(self): print "WithinContext.__del__" class Context(object): def __init__(self): print "Context.__init__()" def __enter__(self): """ 在主体代码执行前执行 """ print "Context.__enter__()" return WithinContext(self) def __exit__(self,exc_type,exc_val,exc_tb): """ 在主体代码执行后执行 """ print "Context.__exit__()" with Context() as c : """ as后面的变量是在__enter__函数中返回的 """ c.do_something() Context.__init__() Context.__enter__() WithinContext.__init__(<__main__.Context object at 0x7f95045167d0>) WithinContext.do_something() Context.__exit__() WithinContext.__del__
|
contextlib中的contextmanager作为装饰器来提供一种针对函数级别的上下文管理机制.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| import contextlib @contextlib.contextmanager def make_context(): print "entering" try: yield {} finally: print "exiting" with make_context() as value: print "inside with statement:", value """ entering inside with statement: {} exiting """ @contextlib.contextmanager def make_context(name): print "entering",name yield name print "exiting",name with contextlib.nested(make_context('A')) as (c): print "inside with statement: ",c """ entering A inside with statement: ['A'] exiting A """ with contextlib.nested(make_context('A'),make_context("B"),make_context("C")) as (A,B,C): """ nested用于创建嵌套的上下文 """ print "inside with statement: ",A,B,C """ entering A entering B entering C inside with statement: A B C exiting C exiting B exiting A """ class Door(object): def __init__(self): print "__init__()" def close(self): print "close()" return with contextlib.closing(Door()) as door: """ closing执行定义好的close函数 """ print "inside with statement." """ __init__() inside with statement. close() """
|