Reputation: 8027
class fcount(object):
def __init__(self, func):
self.func = func
self.count = 0
self.context_count = 0
def __enter__(self):
self.context_count = 0
def __call__(self, *args):
self.count += 1
self.context_count += 1
return self.func(*args)
def __exit__(self, exctype, value, tb):
return False
This is a decorator. The idea is to keep a separate count when using a 'with' block.
If I do this:
@fcount
def f(n):
return n+2
with fcount(foo) as g:
print g(1)
I get this error: TypeError: 'NoneType' object is not callable
I tried printing out the type of g inside that with block, and of course the type is None.
Any idea why g isn't being assigned to fcount(foo)?
This does work:
g = fcount(foo)
with g:
g(1)
Upvotes: 2
Views: 85
Reputation: 798716
You forgot to return the object from __enter__()
.
Upvotes: 2