class a:
@HTMLwrapper
def return_stuff_as_html(self, input):
return self.return_stuff(input)
def return_stuff(self, input):
return awesome_dict
我做同樣的事情在等待響應,它爲我工作得很好,但我仍想知道是否有更好的方法: ) - olofom
由於在python函數和方法中是對象,並且由於裝飾器返回可調用對象,所以可以在裝飾方法上指定一個屬性指向原始方法od,但是像my_object_instance.decorated_method.original_method()這樣的調用會更加醜陋,並且不夠明確。
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
我做了幾乎同樣的事情在等待,但我不允許原來的功能,所以我只是改名returnStuff改變returnStuffHelper和裝飾returnStuff,然後調用而不是我的函數returnStuffHelper。其他代碼需要returnStuff返回HTML。 – olofom 2012-03-14 12:53:05
@olofom:已更新 – 2012-03-14 13:03:32
我不允許更改裝飾器或原始方法,所以我猜這對我來說不會更好。感謝:) – olofom 2012-03-14 14:29:37