そのまんまのメモです
Hook.pyを作るときにデコレータが付いたもの
ついていないものを混ぜて実行できるようにしたくなったので
お試ししてみた。
ソースコード
class Main():
def add_resp(cmd):
def _add_resp(func):
def wrapper(*args, **kwargs):
func.decorated = "Cmd"
return func
return wrapper
return _add_resp
@add_resp("help")
def help(self):
print("test")
def help2(self):
print("test2")
x = Main()
print(dir(x.help()))
print(dir(x.help2()))
実行結果
['__annotations__', ... 'decorated']
test2
['__bool__', ...']
使い方
hasattr(関数, 'decorated')
ってやればデコレートされてるかがわかる
if "decorated" in dir(関数):
でもOK
補足
ひとつでも 関数に変数を追加すると
__getattribute__ っていうのが現れて
それを見ると 辞書型で追加した変数一覧が出る
(追加されてないと Nonetype ~ でエラーが返る)
感想
func.~ で 関数に変数を作成できちゃうんだなぁ~