目录
  • 装饰器
    • 装饰器的定义
    • 装饰器的意义
    • 装饰器的使用
      • 无参装饰器
      • 有参装饰器
  • 实例练习
    • 总结

      装饰器

      装饰器的定义

      关于装饰器的定义,我们先来看一段github上大佬的定义:

      function decorators are simply wrappers to existing functions.
      in the context of design patterns,decorators dynamically alter the functionality of a function, method or class without having to directly use subclasses.
      this is ideal when you need to extend the functionality of functions that you don’t want to modify.
      we can implement the decorator pattern anywhere, but python facilitates the implementation by providing much more expressive features and syntax for that.

      这段话的最主要的意思就是:

      函数装饰器是给已有函数的简单容器。
      在原有的代码环境下,它能够动态地改变一个函数的功能,或者不直接使用子类而改变方法和类。
      当你不想修改源代码但又想实现功能上的扩充的时候,这是一个不错的方法。
      我们可以在任何情况下使用装饰器样式,同时python通过提供更多强力的属性和语法来帮助装饰器的使用。

      装饰器的意义

      在上文之中其实我们已经知道了装饰器的意义,
      可以不需要修改源代码就能够直接做到功能的扩充,但是为此我们需要付出的是更多的编写时间,
      而且更为重要的是,通过修改源码实现功能的增加往往会改变接口传递的参数,可一个项目之中往往存在许多接口这也代表着你可能需要多次更改接口参数,
      这个工作量,我们可不干!

      装饰器的使用

      无参装饰器

      无参装饰器是最为基础的装饰器,
      其根本原因在于装饰的函数对象不需要仍和的参数;
      接下来示范一下最简单的装饰器是如何实现的:

      def greeting():						# 定义一个最基本的无参函数
          return "hello,读者老爷们!"
      # 现在我需要实现的要求是:让输出的内容变为<p>"hello,读者老爷们"<p>
      # 在不直接更改greeting函数的前提下,我们需要使用无参装饰器
      def decorator_p(func):				# 用于接收一个函数
      	def wrapper():
      		return f'<p>{func()}<p>'
      	return wrapper
      decorator = decorator_p(greeting)		# 调用decorator_p,并且用一个decorator接收返回值
      print(decorator())
      

      <p>hello,读者老爷们!<p>
      以上就是输出的结果

      但是这个结果我其实并不满意,
      因为完成了功能附加之后我们居然还需要再使用decorator = decorator_p(greeting)来接收一下,而且这样的话调用方式就不再是原本的greeting()了,而是decorator()
      这两者对于追求高效优雅的python来说已经提供解决方法了,
      让我娓娓道来:

      # 针对于调用方式而言,我们首先想到的解决方法是
      greeting = decorator_p(greeting)		# 将原本的greeting函数传给decorator_p,而重命名
      print(greeting())						# 输出结果与原来相同
      
      但这个仍然可以改进,这就要使用到python提供的@,
      但使用这种方法必须注意书写的顺序,因此代码必须这样更改:
      
      def decorator_p(func):
          def wrapper():
              return f'<p>{func()}<p>'
          return wrapper
      
      @decorator_p					# 效果等同于 greeting = decorator_p(greeting)
      def greeting():
          return "hello,读者老爷们!"
      print(greeting())
      

      <p>hello,读者老爷们!<p>

      得到的结果是我们想要的,但使用这种方法顺序格外重要,
      如果这样书写,则会给出报错:

      @decorator_p						#使用这个@后,将会开始向上寻找decorator_p这个函数
      def greeting():
          return "hello,读者老爷们!"
      print(greeting())
      
      def decorator_p(func):
          def wrapper():
              return f'<p>{func()}<p>'
          return wrapper
      

      nameerror: name ‘decorator_p’ is not defined

      给出的报错原因是因为没有找到decorator_p这个函数,
      可明明我们已经完成这个函数的定义了,
      所以我们可以得到的结论便是:

      当使用@时,就会开始向上寻找函数,当找不到函数的时候就会报错

      有参装饰器

      接下来介绍一下有参装饰器,就是指需要传递参数的装饰器,
      上文之中其实已经介绍过了关于无参装饰器的使用,而有参装饰器也并没有多难,
      来一个示范:

      def decorator(func):
          def wrapper(name):
              return f'<p>{func(name)}<p>' 
          return wrapper
      
      @decorator
      def greeting(name):
          return f"hello,{name}!"
      print(greeting('读者老爷'))		# 传递一个参数
      

      <p>hello,读者老爷!<p>

      实例练习

      ok,经过上文介绍所有读者应该又会处于似懂非懂的状态,
      那么秉持一文一练的理念,我们接下来将通过编写登录功能,仔细看一下使用装饰器和不适用装饰器的区别。
      在开始之前,我创建了一个文本文件夹,将会用于模拟用户储存的信息,内容如下:

      sign_in.txt
      joseph:jostar
      jonasen:jostar
      kujo:jotaro
      jolin:kujo
      diavollo:doppio
      

      需求清单:
      1)用户账户只能够由数字和英文组成,长度不超过16字符;
      2)用户密码只能够由数字和英文组成,长度不超过16字符;
      3)用户拥有4次输入的机会。

      # 不使用任何函数
      # 无装饰器 登录功能
      count = 1				# 用于计算使用次数
      while count < 5:
          user_account = input('请输入你的账号')
          if user_account.isalnum() and len(user_account) <= 16:		# 判断账号长度和组成
              user_keyword = input('请输入你的密码')
              if user_keyword.isalnum() and len(user_keyword) <= 16:	# 判断密码的长度和组成
                  with open('sign_in.txt','r') as file:				# 打开登录文件核对登录信息
                      for line in file:
                          r_name, r_keyword = line.strip().split(':')
                          if r_name == user_account and r_keyword == user_keyword:
                              print('登录成功')
                              break
                      else:
                          count += 1
                          print(f'账号密码不匹配,还剩下{5-count}次机会')
                          continue
                      break
              else:
                  count += 1
                  print(f'密码输入错误,还剩下{5-count}次机会')
                  continue
          else:
              count += 1
              print(f'输入账号错误,还剩下{5-count}次机会')
              continue
      else:
          print('机会已用完')
      

      程序员之间总是流传着这么一个梗,
      开发总是会看着一串代码愣愣出神说道:“这究竟是谁写的代码,像坨屎。”
      然后过了良久突然说“好像是我自己写的。”

      接下来我们需要做的就是改良,使用装饰器将其改良,
      我们的源代码将其固定成这样:

      # 只是单纯的用户输入,
      # 而后我们要在此基础上不断优化,为其添加上判断长度、限制次数的功能
      def input_signup():
          user_name = input('请输入账户名字')
          user_keyword = input('请输入账户密码')
          return user_name, user_keyword
      
      def passing_func(func1, func2, func3):
          def wrapper(func4):
              def decorator():
                  count = 1
                  while count < 5:
                      name, keyword = func4()
                      check = func1(name, keyword)
                      if check is not true:
                          count += 1
                          print(f'你还剩下{5-count}次登录机会')
                          continue
                      check = func2(name, keyword)
                      if check is not true:
                          count += 1
                          print(f'你还剩下{5-count}次登录机会')
                          continue
                      check = func3(name, keyword)
                      if check is not true:
                          count += 1
                          print(f'你还剩下{5 - count}次登录机会')
                          continue
                      else:
                          break
              return decorator
          return wrapper
      
      
      def limit_len(name, keyword, length=16):
          '''
          用于判断用户名字和密码的长度是否符合标准
          :param   name:用于接受用户的名字
          :param   keyword:用于接受用户的密码
          :param   length:默认参数为16
          :return: check:用于判断输入是否符合长度标准,详细请见装饰器中的使用
          '''
      
          check = true
          if len(name) > length or len(keyword) > length:
              print('账号名或密码长度不符合规范')
              check = false
              return check
          return check
      
      
      def limit_composition(name, keyword):
          '''
          用于判断用户名字和密码的组成是否符合标准
          :param   name:用于接受用户的名字
          :param   keyword:用于接受用户的密码
          :return: check:用于判断输入是否符合长度标准,详细请见装饰器中的使用
          '''
      
          check = true
          if name.isalnum() is not true or keyword.isalnum() is not true:
              print('账号名或密码组成不符合规范')
              check = false
              return check
          return check
      
      
      def verify_useinfo(name, keyword):
          '''
          用于检验用户输入的账号和密码是否符合文件中储存的
          :param   name:用于接受用户的名字
          :param   keyword:用于接受用户的密码
          :return: check:用于判断输入是否符合长度标准,详细请见装饰器中的使用
          '''
          check = true
          with open('sign_in.txt','r') as file:
              for line in file:
                  r_name, r_keyword = line.strip().split(':')
                  if r_name == name and r_keyword == keyword:
                      print('登录成功,欢迎使用')
                      return check
              else:
                  check = false
                  print('账号密码错误,请重新输入')
                  return check
      
      
      @passing_func(limit_len, limit_composition, verify_useinfo)
      def input_signup():
          user_name = input('请输入账户名字')
          user_keyword = input('请输入账户密码')
          return user_name, user_keyword
      
      

      以上就是关于装饰器的所有内容了,
      希望能够对所有读者有所帮助。

      总结

      到此这篇关于python函数装饰器的使用详解的文章就介绍到这了,更多相关python函数装饰器内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!