【问题标题】:How to skip or ignore python decorators如何跳过或忽略 python 装饰器
【发布时间】:2012-03-30 22:15:25
【问题描述】:

有一个由装饰器包装的函数,它以 HTML 形式返回函数的输出。我想在没有装饰器的 HTML 包装的情况下调用该函数。这可能吗?

例子:

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.

【问题讨论】:

    标签: python decorator


    【解决方案1】:
    class a:
        @HTMLwrapper
        def return_stuff_as_html(self, input):
            return self.return_stuff(input)
        def return_stuff(self, input):
            return awesome_dict
    

    我在等待回复时做了同样的事情,它对我来说很好,但我仍然想知道是否有更好的方法:) – olofom

    由于在 python 中函数和方法都是对象,并且由于装饰器返回一个可调用对象,您可以在装饰方法上设置一个指向原始方法的属性,但是像 my_object_instance.decorated_method.original_method() 这样的调用会更丑陋而且更少明确的。

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

    【讨论】:

    • 我在等待时做了几乎同样的事情,但是我不允许更改原始函数,所以我只是将 returnStuff 重命名为 returnStuffHelper 并装饰了 returnStuff,然后在我的函数中调用 returnStuffHelper。其他代码需要 returnStuff 才能返回 HTML。
    • 我不允许更改装饰器或原始方法,所以我想这对我来说不会比这更好。谢谢:)
    • 为什么不return_stuff_as_html = HTMLwrapper(return_stuff)
    • 你应该发布自己的答案,oloform。
    【解决方案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)
    

    【讨论】:

    • 不错的例子,进入我的小sn-ps库。
    • :D YAY 我喜欢待在这些模块中。
    • 这很棒。非常有帮助的sn-p。
    【解决方案3】:

    当然:

    class Example(object):
        def _implementation(self):
            return something_awesome()
    
        returnStuff = HTMLwrapper(_implementation)
    
        def callStuff(self):
            do_something_with(self._implementation())
    

    【讨论】:

    • 我不能改变修饰函数,其他代码依赖于它。我改用 Paulo Scardine 的代码。
    猜你喜欢
    • 2012-08-07
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2018-11-26
    • 1970-01-01
    • 2022-01-21
    • 2015-02-02
    相关资源
    最近更新 更多