python装饰器两层和三层区别,Python装饰器和装饰器图案有什么区别?

本文探讨了Python装饰器与装饰器模式之间的差异。Python装饰器在定义时增强函数或方法的功能,而装饰器模式则允许在运行时动态地扩展对象的行为。虽然它们不完全相同,但Python装饰器可以视为装饰器模式的一种实现,只是方式不同。文中通过实例展示了两种装饰器的用法,并指出了何时使用Python装饰器和装饰器模式。
摘要由CSDN通过智能技术生成

What is the difference between “Python decorators” and the “decorator pattern”?

When should I use Python decorators, and when should I use the 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.

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.

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.

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

解决方案

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.

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 -

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值