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

-- 
Wolfgang
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to