Python装饰器和装饰器模式之间的区别是什么?
“Python装饰器”和“装饰器模式”之间的区别是什么 什么时候应该使用Python装饰器,什么时候应该使用装饰器模式 我正在寻找Python装饰器和实现相同功能的装饰器模式的示例 @AcceptedAnswer 我知道这是正确的。然而,正是斯里卡尔的回答让我明白了原因 在Srikar给出答案并研究了给定的资源之后,我编写了这个示例,这样我就可以可视化和理解Python装饰器和装饰器模式 我不同意Srikar的“Python装饰器不是装饰器模式的实现”。经过我的学习,我坚信Python decorator是decorator模式的实现。只是不是以经典的方式 同样,我需要补充一点,尽管Srikar说“Python decorators在定义时向函数和方法添加功能,您可以在运行时轻松使用Python decoratorsPython装饰器和装饰器模式之间的区别是什么?,python,design-patterns,decorator,Python,Design Patterns,Decorator,“Python装饰器”和“装饰器模式”之间的区别是什么 什么时候应该使用Python装饰器,什么时候应该使用装饰器模式 我正在寻找Python装饰器和实现相同功能的装饰器模式的示例 @AcceptedAnswer 我知道这是正确的。然而,正是斯里卡尔的回答让我明白了原因 在Srikar给出答案并研究了给定的资源之后,我编写了这个示例,这样我就可以可视化和理解Python装饰器和装饰器模式 我不同意Srikar的“Python装饰器不是装饰器模式的实现”。经过我的学习,我坚信Python deco
"""
Testing Python decorators against the 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 function
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 the decorator pattern
然而,我仍然认为Srikar的回答是可以接受的,因为它帮助我理解Python中装饰器模式的
"""
Testing Python decorators against the 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 function
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 the decorator pattern
装饰器模式在面向对象编程中,装饰器模式是一种设计模式,允许将行为动态添加到现有对象中。decorator模式可用于在运行时扩展(装饰)某个对象的功能,独立于同一类的其他实例,前提是在设计时做了一些基础工作 Python中的decorator——尽管名称不同,Python decorator并不是decorator模式的实现。decorator模式是静态类型的面向对象编程语言中使用的一种设计模式,允许在运行时向对象添加功能;Python装饰器在定义时向函数和方法添加功能,因此是比装饰器模式类更高级的构造 装饰器模式本身在Python中很容易实现,因为该语言是duck类型的,因此通常不被认为是duck类型的。因此,在Python中,decorator是用于修改函数、方法或类定义的任何可调用Python对象 我希望我把区别讲清楚了。万一你不完全理解,请浏览这些链接。最后你会明白的-
- &
@modifyBehavior
def original(myString):
print myString
原始文件的行为被覆盖。您不能使用此添加新功能
(b) 装饰器模式是关于多态性的。在上面的示例代码中,Decorator.something\u的行为被覆盖。原来的方法丢失了。这不是真正的装饰图案。您应该寻求增强或添加功能,而不是替换方法。您应该确保a.something\u usupable(字符串)返回的内容与b.something\u usupable(字符串)返回的内容相同。事实上,在decorator模式中,您通常会替换原始对象。我的意思是:
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 withUnderscores(self, string):
return '_'.join(string.split(' '))
def __getattr__(self, name):
return getattr(self._wrapped, name)
if __name__ == '__main__':
string = 'Lorem ipsum dolor sit amet.'
obj = Class()
print('Original: ', obj.something_useful(string))
#This has no underscore function. Use decorator to add.
obj = Decorator(obj)
print('Replaced spaces: ', obj.withUnderscores(string))
print('Original still works: ', obj.something_useful(string))
您可以使用多个装饰器来添加功能。这允许您在需要时只添加所需内容。更多阅读:@Srikar是正确的。”这是另一个你可能会觉得有趣的问题@Srikar,我不能接受不能解决问题的Snswer,抱歉,但我问题中提出的大多数解决方案都不起作用。FWIW,根据维基百科关于“尽管名称不同,Python装饰器不是装饰器模式的实现”的文章。它接着解释了原因。谢谢@apcelent。这可能有助于任何人理解主题;)斯里卡尔的答案绝对正确。decorator不适用于运行时修改。我的回答有点细微差别,但他的回答是最好的。