14

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.
AstroCB
  • 12,337
  • 20
  • 57
  • 73
olofom
  • 6,233
  • 11
  • 37
  • 50

3 Answers3

5
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!
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • I did almost the same thing while waiting, but I was not allowed to change the original function so I just renamed returnStuff to returnStuffHelper and decorates returnStuff and then calls returnStuffHelper in my function instead. Other code needs the returnStuff to return HTML. – olofom Mar 14 '12 at 12:53
  • I'm not allowed to change either the decorator or the original method so I guess it won't be better than this for me. Thanks :) – olofom Mar 14 '12 at 14:29
  • why not `return_stuff_as_html = HTMLwrapper(return_stuff)` – Jiaaro Mar 14 '12 at 18:33
  • You should post your own answer, oloform. – sleblanc Feb 23 '13 at 20:07
5
__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)
Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
0

Sure:

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

    returnStuff = HTMLwrapper(_implementation)

    def callStuff(self):
        do_something_with(self._implementation())
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • I'm not allowed to change the decorated function, other code is dependent on it. I went with Paulo Scardine's code instead. – olofom Mar 14 '12 at 12:57