2012-03-14 43 views
10

Có một hàm được bao bọc bởi một trình trang trí trả về đầu ra của hàm dưới dạng HTML. Tôi muốn gọi hàm đó mà không cần gói HTML của trình trang trí. Điều đó thậm chí có thể?Cách bỏ qua hoặc bỏ qua trang trí python

Ví dụ:

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. 

Trả lời

4
class a: 
    @HTMLwrapper 
    def return_stuff_as_html(self, input): 
     return self.return_stuff(input) 
    def return_stuff(self, input): 
     return awesome_dict 

tôi đã làm điều tương tự trong khi chờ đợi một câu trả lời và nó hoạt động tốt đối với tôi, nhưng tôi vẫn muốn biết nếu có một cách tốt hơn:) - olofom

Vì trong các hàm và phương thức python là các đối tượng và vì trình trang trí trả về một cuộc gọi, bạn có thể đặt thuộc tính trên phương thức được trang trí trỏ đến bản gốc od, nhưng một cuộc gọi như my_object_instance.decorated_method.original_method() sẽ xấu hơn và ít rõ ràng hơn.

>>> 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

tôi đã gần như điều tương tự trong khi chờ đợi, nhưng tôi không được phép thay đổi các chức năng ban đầu vì vậy tôi chỉ cần đổi tên returnStuff để returnStuffHelper và trang trí returnStuff và sau đó cuộc gọi returnStuffHelper trong hàm của tôi để thay thế. Mã khác cần returnStuff để trả về HTML. – olofom

+0

@olofom: cập nhật –

+0

Tôi không được phép thay đổi trang trí hoặc phương pháp gốc vì vậy tôi đoán nó sẽ không tốt hơn điều này đối với tôi. Cảm ơn :) – olofom

0

chắc:

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

    returnStuff = HTMLwrapper(_implementation) 

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

Tôi không được phép thay đổi chức năng được trang trí, mã khác phụ thuộc vào nó. Tôi đã đi với mã của Paulo Scardine để thay thế. – 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

Ví dụ hay, trong thư viện đoạn trích nhỏ của tôi. – ohmi

+0

: D YAY I I love being in the modules. –

+0

Điều này thật tuyệt. Đoạn trích rất hữu ích. – krishnab