Decorators and recursive functions

1. decorator II

  • In the decorator I, one of the decorative application, you can follow in the open closed principle premise of adding new features. However, this method is imperfect added, namely: decorative multiple functions, must "Category talk" can not be aggregated, does not have the coupling.

  • A plurality of decorator decorative function (parameter presence decorator): Enhanced coupling code.

  • The basic form of a decorator with parameters:

    def outer(n):
        def middle(x):
            def inner(*args,**kwargs):
                ……
                调用 n
                a = x(*args,**kwargs)
                return a
              retrun inner
        return middle
    @outer("实参")
    def func():
        pass
    func()
    • Example:

      def wrapper_out(n):
          def wrapper(f):
              def inner(*args, **kwargs):
                  if n == 'qq':
                      username = input('请输入用户名:').strip()
                      password = input('请输入密码:').strip()
                      with open('qq', encoding='utf-8') as f1:
                          for line in f1:
                              user, pwd = line.strip().split('|')
                              if username == user and password == pwd:
                                  print('登陆成功')
                                  ret = f(*args, **kwargs)
                                  return ret
                          return False
                  elif n == 'tiktok':
                      username = input('请输入用户名:').strip()
                      password = input('请输入密码:').strip()
                      with open('tiktok', encoding='utf-8') as f1:
                          for line in f1:
                              user, pwd = line.strip().split('|')
                              if username == user and password == pwd:
                                  print('登陆成功')
                                  ret = f(*args, **kwargs)
                                  return ret
                          return False
                  username = input('请输入用户名:').strip()
                  password = input('请输入密码:').strip()
                  with open(n, encoding='utf-8') as f1:
                      for line in f1:
                          user, pwd = line.strip().split('|')
                          if username == user and password == pwd:
                              print('登陆成功')
                              ret = f(*args, **kwargs)
                              return ret
                      return False
              return inner
          return wrapper
      
      @wrapper_out('qq')
      def qq():
          print('成功访问qq')
      qq()
      
      @wrapper_out('tiktok')
      def tiktok():
          print('成功访问抖音')
      tiktok()
      • Can be seen by way of example, by an additional decorative layer containing function sets the parameter, the effect can be achieved polymerizable some code duplication. This additional effect complete intermediate variables, is the parameter n.
      • @ Outermost layer of the function name ( "arguments") is a decorator I, expand the outermost layer of the function name @. It represents:
        • Outermost decorative function is the function name = (decorated function), this time, when invoked command functions to be decorated, @ ~ were translated, performed at this time is a function of the middle, and at this time, to achieve a parameter passing.
        • Continue to follow this line of thought away, and the remaining steps will be the "standard model" the same.
      • However, this idea still has its own drawbacks, is to encounter each function to be decorated, one should write @ ~, according to this explanation coupling of the code can not do the most extreme.

      • Recursion, in general, the equivalent of an infinite while loop. But the presence of protected memory operating systems, there is a maximum number of recursion. The default maximum number of recursive official is: 1000.
      • The reason is that the establishment of protection mechanisms, each call to the function, will open up new space in memory, while the original space because the function is not executed, has not been released.
      • In principle, the number of recursions 100 is not to be redundant, for efficiency, time and complexity.
      • General recursive problem can be solved with if and while can get, using a recursive sometimes wonders.

Guess you like

Origin www.cnblogs.com/W-Y-C/p/11086810.html