###前提・実現したいこと
Python でclass内にdecoratorを持たせたい
###発生している問題・エラーメッセージ
Traceback (most recent call last): File "deco2.py", line 1, in <module> class MyClass(object): File "deco2.py", line 12, in MyClass @decorator TypeError: decorator() takes exactly 2 arguments (1 given)
###該当のソースコード
python
1class MyClass(object): 2 def __init__(self): 3 self.my_var = "my_var" # 追加しました 4 # pass 5 6 def decorator(self, func): 7 def inner(*args, **kwargs): 8 print "pre func" 9 print self.my_var # 追加しました 10 func(*args, **kwargs) 11 print "after_func" 12 return inner 13 14 @decorator #ここでエラーになります 15 def print_hoge(self): 16 print "hoge" 17 18 19temp = MyClass() 20temp.print_hoge()
###試したこと
python
1def decorator(func): 2 def inner(*args, **kwargs): 3 print "pre func" 4 func(*args, **kwargs) 5 print "after_func" 6 return inner 7 8 9@decorator 10def print_hoge(): 11 print "hoge" 12 13print_hoge() 14 15>>pre_func 16>>hoge 17>>after_func
###補足情報(言語/FW/ツール等のバージョンなど)
いつも参考にさせていただいております。
非常に簡単な質問かも知れないのですが、実力が足りず解決方法が分からない為教えてください。
【試したこと】の欄のソースでは上手くいくのに、class内に入れた際うまく行きません。
よろしくお願い致します。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/21 01:47
2016/10/21 03:20
2016/10/21 06:45