本文介绍了Python装饰器和装饰器图案有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python装饰器和装饰器图案之间有什么区别?

What is the difference between "Python decorators" and the "decorator pattern"?

什么时候我应该使用Python装饰器,什么时候应该使用装饰图案?

When should I use Python decorators, and when should I use the decorator pattern?

我正在寻找Python装饰器和装饰器模式的例子完成相同?




@AcceptedAnswer


我知道是有效的。然而,Strikar的答案让我明白为什么。




在Srikar的回答和学习资源之后,我写了这个例子,所以我可以可视化和理解Python装饰器和Decorator Pattern。

I'm looking for examples of Python decorators and the decorator pattern accomplishing same?


@AcceptedAnswer
I know that Jakob Bowyer's answer is valid. Yet is's Strikar's answer that made me understand why.


After Srikar's answer and studying given resources I've written this example, so I can visualize and understand Python decorators and Decorator Pattern.

我必须不同意与Strikar的 Python装饰器不是装饰器模式的实现。在我所学到的知识之后,我非常相信Python装饰器是Decorator Pattern的实现。只是不是以经典的方式。

I must disagree with Strikar's "Python decorators are not an implementation of the decorator pattern". After what I've learned I'm strongly convinced that Python decorators are implementation of Decorator Pattern. Just not in the classic way.

,我需要补充一点,尽管Strikar表示:Python定制器在定义时添加功能和方法可以轻松地在运行时使用Pytohon装饰器时间即可。

Also, I need to add that despite the fact that Strikar said "Python decorators add functionality to functions and methods at definition time" you can easily use Pytohon decorators at run time.

然而,我仍然把Stiker的答案标记为被接受,因为它帮助我理解了 在Python中的Decorator Pattern

Yet, I still mark Stiker's answer as accepted, because it helped me understand Implementation of Decorator Pattern in Python.

"""
Testing Python decorators against Decorator Pattern
"""
def function(string):
    return string

def decorator(wrapped):
    def wrap(string):
        # assume that this is something useful
        return wrapped(string.upper())
    return wrap

def method_decorator(wrapped):
    def wrap(instance, string):
        # assume that this is something useful
        return wrapped(instance, string.upper())
    return wrap

@decorator
def decorated_function(string):
    print('! '.join(string.split(' ')))

class Class(object):
    def __init__(self):
        pass
    def something_useful(self, string):
        return string

class Decorator(object):
    def __init__(self, wrapped):
        self.wrapped = wrapped
    def something_useful(self, string):
        string = '! '.join(string.split(' '))
        return self.wrapped().something_useful(string)

    @method_decorator
    def decorated_and_useful(self,string):
        return self.something_useful(string)


if __name__ == '__main__':
    string = 'Lorem ipsum dolor sit amet.'
    print(function(string))                  # plain functioon
    print(decorator(function)(string))       # Python decorator at run time
    print(decorated_function(string))        # Python decorator at definition time
    a = Class()
    print(a.something_useful(string))        # plain method
    b = Decorator(Class)
    print(b.something_useful(string))        # Decorator Pattern
    print(b.decorated_and_useful(string))    # Python decorator decorated Decorator Pattern


推荐答案

p> 装饰模式 - 在面向对象编程中,装饰器模式n是允许动态地将行为添加到现有对象的设计模式。只要在设计时完成了一些基础工作,就可以在运行时使用装饰器模式来扩展(装饰)某个对象的功能,而不必在同一个类的其他实例中。

Decorator Pattern - In object-oriented programming, the decorator pattern is a design pattern that allows behaviour to be added to an existing object dynamically. The decorator pattern can be used to extend (decorate) the functionality of a certain object at run-time, independently of other instances of the same class, provided some groundwork is done at design time.

Python中的装饰器 - 尽管名称,Python装饰器不是装饰器模式的实现。装饰器图案是用于静态类型的面向对象编程语言的设计模式,以允许在运行时将功能添加到对象; Python装饰器在定义时添加功能和功能,因此是比decorator-pattern类更高级的构造。装饰器模式本身在Python中是可以实现的,因为这种语言是鸭型的,所以通常不被认为是这样的。所以在Python中,装饰器是用于修改函数,方法或类定义的任何可调用的Python对象。

Decorators in Python - Despite the name, Python decorators are not an implementation of the decorator pattern. The decorator pattern is a design pattern used in statically typed object-oriented programming languages to allow functionality to be added to objects at run time; Python decorators add functionality to functions and methods at definition time, and thus are a higher-level construct than decorator-pattern classes. The decorator pattern itself is trivially implementable in Python, because the language is duck typed, and so is not usually considered as such. So in Python a decorator is any callable Python object that is used to modify a function, method or class definition.

希望我有所区别。为了防止你不完全明白,请通过这些链接。你会在结束时出来更清楚 -

I hope i made the difference clear. Just in case you did not completely understand, please go through these links. You will come out more than clear at the end of it -

&

这篇关于Python装饰器和装饰器图案有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:22