2012-03-14 29 views
10

Istnieje funkcja, która jest zawijana przez dekorator, który zwraca dane wyjściowe funkcji jako HTML. Chciałbym nazwać tę funkcję bez zawijania HTML dekoratora. Czy to w ogóle jest możliwe?Jak pominąć lub zignorować konstruktory pytonów

Przykład:

class a: 
    @HTMLwrapper 
    def returnStuff(input): 
     return awesome_dict 

    def callStuff(): 
     # here I want to call returnStuff without the @HTMLwrapper, 
     # i just want the awesome dict. 

Odpowiedz

4
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! 
+0

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

+0

@olofom: zaktualizowano –

+0

Nie wolno mi zmieniać ani dekoratora, ani oryginalnej metody, więc myślę, że nie będzie to dla mnie lepsze. Dzięki :) – olofom

0

Sure:

class Example(object): 
    def _implementation(self): 
     return something_awesome() 

    returnStuff = HTMLwrapper(_implementation) 

    def callStuff(self): 
     do_something_with(self._implementation()) 
+0

Nie wolno mi zmieniać dekorowanej funkcji, inny kod jest od niej zależny. Zamiast tego poszedłem z kodem Paulo Scardine. – olofom

2
__author__ = 'Jakob' 

class OptionalDecoratorDecorator(object): 
    def __init__(self, decorator): 
     self.deco = decorator 

    def __call__(self, func): 
     self.deco = self.deco(func) 
     self.func = func 
     def wrapped(*args, **kwargs): 
      if kwargs.get("no_deco") is True: 
       return self.func() 
      else: 
       return self.deco() 
     return wrapped 

def spammer(func): 
    def wrapped(): 
     print "spam" 
     return func() 
    return wrapped 

@OptionalDecoratorDecorator(spammer) 
def test(): 
    print "foo" 

test() 
print "***" 
test(no_deco=True) 
+0

Dobry przykład, przechodzenie w mojej bibliotece małych fragmentów. – ohmi

+0

: D YAY Uwielbiam być w tych modułach. –

+0

To jest świetne. Bardzo pomocny fragment. – krishnab