クラス内の各関数の最初と最後に何か簡単な方法がありますか?私は__getattribute__
を調べましたが、私はこの状況で使うことはできないと思いますか?メソッドの始めと終わりに何かをする
ここで私が何をしようとしているの簡易版です。
def decorator(method):
def decorated_method(self, *args, **kwargs):
# before the method call
if self.busy:
return None
self.busy = True
# the actual method call
result = method(self, *args, **kwargs)
# after the method call
self.busy = False
return result
return decorated_method
class Thing():
def __init__(self):
self.busy = False
@decorator
def func_1(self):
...
@decorator
def func_2(self):
...
:
class Thing():
def __init__(self):
self.busy = False
def func_1(self):
if self.busy:
return None
self.busy = True
...
self.busy = False
def func_2(self):
if self.busy:
return None
self.busy = True
...
self.busy = False
...
これはどうしてやりたいのですか? – abccd
@abccdなぜ、彼はそれのような 'self.busy'を使いたいのですか? –
はい、それは私が意味したものです – abccd