详解Python装饰器的四种定义形式

 

前言

装饰器(decorator)在Python框架中扮演着重要角色,是Python中实现切面编程(AOP)的重要手段。

aspect-oriented programming (AOP) ,在不改变代码自身的前提下增加程序功能

不改变代码自身,但需要在函数和类头上加一个标注(annotation),这个标注在Python里叫装饰器,在java里叫注解。
在Python里,一共有四种组合形式。下面一一举例。

 

用函数装饰函数

采用一个函数定义装饰器:

def decorate(f):
  def wrapper(*args):
      return f(*args)*2
  return wrapper

然后作用在一个函数上:

@decorate
def add(a, b):
	return a + b

测试一下效果:

def test_decorate():
	sum = add(3, 5)
	assert sum == 16

 

用函数装饰一个类

这里通过装饰器实现单例模式:

def singleton(cls):
  instances = {}
  def wrapper(*args, **kwargs):
      if cls not in instances:
        instances[cls] = cls(*args, **kwargs)
      return instances[cls]
  return wrapper

使用该装饰器:

@singleton
class MyClass:
  def method(self):
      pass

于是,当你定义多个对象时,返回的是同一实例:

obj = MyClass()  # creates a new instance
obj2 = MyClass()  # returns the same instance
obj3 = MyClass()  # returns the same instance
...

 

用类定义装饰器,然后装饰一个函数

先采用类定义一个装饰器:

class Star:
  def __init__(self, n):
      self.n = n

  def __call__(self, fn):
      @wraps(fn)
      def wrapper(*args, **kwargs):
          result = fn(*args, **kwargs)
          return result
      return wrapper

再作用在一个函数上:

@Star(5)
def add(a, b):
  return a + b

主要是在类中实现__call__方法。上面例子也可以简化:

class MyDecorator:
  def __init__(self, function):
      self.function = function
   
  def __call__(self, *args, **kwargs):

      # We can add some code
      # before function call

      self.function(*args, **kwargs)

      # We can also add some code
      # after function call.
# adding class decorator to the function
@MyDecorator
def function(name, message ='Hello'):
  print("{}, {}".format(message, name))

 

用类定义装饰器,然后装饰一个类

先定义装饰器:

class MyClassDecorator(object):
	_instances = dict()

	def __init__(self, name):
		pass

	def __call__(self, cls):
		class WrappedClass(cls):
			def say_hello(self):
				print(f'Hello: {self.username}')
		return WrappedClass

该装饰器给被装饰的类上添加了一个方法,名称为say_hello()。使用如下:

@MyClassDecorator('test')
class MyClass():
	def __init__(self, username):
		self.username = username

然后:

def test_decoratorforclass():
	obj = MyClass('user1')
	obj.say_hello()

打印出: Hello: user1

 

小结

学习类装饰,对Python的内部机制会有更多的了解。如__init__, call, __new__等内置方法。

关于Python装饰器的四种定义形式的文章就介绍至此,更多相关Python装饰器内容请搜索编程宝库以前的文章,希望以后支持编程宝库

Python选择结构怎么实现:本文讲解"Python选择结构如何实现",希望能够解决相关问题。1、选择结构通过判断条件是否成立来决定分支的执行。2、选择结构形式:单分支、双分支、多分支。3、多分支结构,几个分支之间有逻辑关系,不能随意颠倒顺 ...