class a:
@HTMLwrapper
def return_stuff_as_html(self, input):
return self.return_stuff(input)
def return_stuff(self, input):
return awesome_dict
zrobiłem to samo, czekając na odpowiedź i to działa dobrze dla mnie, ale to wciąż chciałbym wiedzieć, czy jest jeszcze lepszy sposób:) - olofom
Ponieważ w python funkcje i metody są obiektami, a ponieważ dekorator zwraca wartość wywoływalną, można ustawić atrybut na dekorowanej metodzie wskazując na oryginalny met od, ale wywołanie takie jak my_object_instance.decorated_method.original_method() byłoby brzydsze i mniej wyraźne.
>>> 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!
zrobiłem prawie to samo, czekając, ale nie wolno było zmienić pierwotną funkcję, więc po prostu przemianowany returnStuff do returnStuffHelper i ozdabia returnStuff a następnie wywołuje returnStuffHelper w mojej funkcji. Inny kod wymaga returnStuff do zwrócenia kodu HTML. – olofom
@olofom: zaktualizowano –
Nie wolno mi zmieniać ani dekoratora, ani oryginalnej metody, więc myślę, że nie będzie to dla mnie lepsze. Dzięki :) – olofom