Re: How can I tell if I am inside a context manager?

2011-02-02 Thread alex23
Gerald Britton gerald.brit...@gmail.com wrote: x = f() hasattr(x, '__enter__') True with f() as x:         hasattr(x,'__enter__') True As you can see, the object has a '__enter__' method regardless of how it was created.  Whatever the test, it needs to return False in the first case

Re: How can I tell if I am inside a context manager?

2011-02-02 Thread alex23
On Feb 2, 1:28 am, Gerald Britton gerald.brit...@gmail.com wrote: I'd like to know how (perhaps with the inspect module) I can tell if I am running in a context manager. Actually, it occurs to me the simplest way is to use the context manager itself to keep track: class F(object): def

How can I tell if I am inside a context manager?

2011-02-01 Thread Gerald Britton
I'd like to know how (perhaps with the inspect module) I can tell if I am running in a context manager. e.g. class f(): def __init__(s): pass def __enter__(s): return s def __exit__(s,a,b,c): return None def g(): x = f() # insert code here to return False, since I am not in

Re: How can I tell if I am inside a context manager?

2011-02-01 Thread Wolfgang Rohdewald
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

How can I tell if I am inside a context manager?

2011-02-01 Thread Gerald Britton
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

Re: How can I tell if I am inside a context manager?

2011-02-01 Thread Jerry Hill
On Tue, Feb 1, 2011 at 11:34 AM, Gerald Britton gerald.brit...@gmail.comwrote: x = open('somefile') # return false since not in a context with open('somefile') as x # return true since in a context. Perhaps something like this: x = open('somefile') if hasattr(x, '__enter__'): return

Re: How can I tell if I am inside a context manager?

2011-02-01 Thread Gerald Britton
Perhaps something like this: x = open('somefile') if hasattr(x, '__enter__'): return false with open('somefile') as x: do_something() class f(): def __init__(s): pass def __enter__(s): return s def __exit__(s,a,b,c): return None x = f() hasattr(x, '__enter__') True with

Re: How can I tell if I am inside a context manager?

2011-02-01 Thread Jerry Hill
On Tue, Feb 1, 2011 at 1:38 PM, Gerald Britton gerald.brit...@gmail.comwrote: As you can see, the object has a '__enter__' method regardless of how it was created. Whatever the test, it needs to return False in the first case and True in the second case, without modifying the class

Re: How can I tell if I am inside a context manager?

2011-02-01 Thread Ethan Furman
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. What's your use-case? ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list