2
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
これはデコレータです。考え方は、 'with'ブロックを使用するときに別のカウントを保持することです。私はこれを行う場合はブロックで使用するときPython定義されていないエラー
:「NoneType」オブジェクトは、私はブロックとその内部のグラムの種類をプリントアウトしようとした呼び出し可能
ではないとの: TypeError例外を:
@fcount
def f(n):
return n+2
with fcount(foo) as g:
print g(1)
私はこのエラーを取得しますコースタイプはNoneです。
gがfcount(foo)に割り当てられていない理由は何ですか?
g = fcount(foo)
with g:
g(1)