Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip or ignore python decorators

There's a function that is wrapped by a decorator that returns the output of the function as HTML. I'd like to call that function without the HTML-wrapping of the decorator. Is that even possible?

Example:

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.
like image 586
olofom Avatar asked Mar 14 '12 12:03

olofom


1 Answers

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

I did the same thing while waiting for a response and it works fine for me, but I'd still like to know if there's an even better way :) – olofom

Since in python functions and methods are objects, and since a decorator returns a callable, you could set an attribute on the decorated method pointing to original method, but a call like my_object_instance.decorated_method.original_method() would be uglier and less explicit.

>>> 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!
like image 145
Paulo Scardine Avatar answered Sep 22 '22 11:09

Paulo Scardine