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
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
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
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
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
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
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
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
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