0
A
回答
2
下面的例子演示瞭如何做到這一點:
def class_dec(starts_with,fun_dec):
def fun(cls):
for k,v in cls.__dict__.items():
if k.startswith(starts_with):
cls.__dict__[k] = fun_dec(v)
return cls
return fun
def fun_decorator(f):
def dec(*args,**kwargs):
print "I m decorating"
f(*args,**kwargs)
return dec
@class_dec("name",fun_decorator)
class Hello:
def name_new_one(self):
print "new one"
def name(self):
print "Hello"
h = Hello()
h.name()
h.name_new_one()
相關問題
- 1. python中的子類方法裝飾器
- 2. Python類方法裝飾
- 3. 在Python中裝飾方法
- 4. 用Python中的裝飾器創建動態方法
- 5. 裝飾類裝飾類方法
- 6. 靜態方法和類方法裝飾器可以在純Python中實現嗎?
- 7. 如何裝飾類或靜態方法
- 8. python2中的類方法鉤裝飾器
- 9. 修改綁定方法及其類的Python裝飾器狀態
- 10. 類裝飾用類方法
- 11. 在Python中裝飾實例方法
- 12. 裝飾類的方法
- 13. 裝飾方法在Python
- 14. Python類裝飾
- 15. Python - 使用類方法來裝飾其他類方法
- 16. 在類方法和子類方法上使用python裝飾器
- 17. 如何在Python中找到裝飾方法的包含類
- 18. Python裝飾類中的方法和繼承
- 19. Python的裝飾類
- 20. 裝飾子類方法
- 21. 裝飾類或dispatch()方法?
- 22. 裝飾父類方法
- 23. 裝飾Python類方法的最佳方法是什麼?
- 24. Python:裝飾靜態方法recive不可調用方法
- 25. Python的動態通過裝飾
- 26. Python的動態類方法
- 27. 爲什麼@decorator不能裝飾靜態方法或類方法?
- 28. Python 2.5中的類裝飾器?
- 29. 如何正確裝飾python中的類
- 30. Python - 使用類中的裝飾函數
我得到這個錯誤:類型錯誤:「dictproxy」對象不支持項目分配 這似乎有效。也許不用裝飾器,我可以裝飾方法,每次調用它們?在方法調用之前調用什麼python魔術方法? – mohi666 2013-03-22 17:23:50