On Dienstag 01 Februar 2011, Gerald Britton wrote: > I'd like to know how (perhaps with the inspect module) I can > tell if I am running in a context manager.
>>class f(object): >> def __init__(self): >> self.inContext = False >> def __enter__(self): >> self.inContext = True >> return self >> def __exit__(self,a,b,c): >> self.inContext = False >> return None >> x = f() >> print 'not within:', x.inContext >> with f() as h: >> print 'within:', h.inContext yes, of course, but in this case I may not modify the class. try it with open: x = open('somefile') # return false since not in a context with open('somefile') as x # return true since in a context. -- Wolfgang -- Gerald Britton -- http://mail.python.org/mailman/listinfo/python-list