title: Can python decorators be used to change behavior of a function returned by a function?
body: <p>I have functions, that return validator functions, simple example:</p>
  
  <pre><code>def check_len(n):
      return lambda s: len(s) == n
  </code></pre>
  
  <p>Is it possible to add a decorator, that prints out a message, in case the check evaluates to false?
  Something like this:</p>
  
  <pre><code>@log_false_but_how
  def check_len(n):
      return lambda s: len(s) == n
  
  check_one = check_len(1)
  print(check_one('a')) # returns True
  print(check_one('abc')) # return False
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>True
  validator evaluated to False
  False
  </code></pre>
  
  <p>I've tried creating an annotation, but can only access the function creation with it.
  One way would be to define the functions like this:</p>
  
  <pre><code>def log_false(fn):
      def inner(*args):
          res = fn(*args)
          if not res:
              print("validation failed for {}".format(fn.__name__))
          return res
      return inner
  
  
  @log_false
  def check_one(s):
      return check_len(1)(s)
  </code></pre>
  
  <p>But this way we lose the dynamic creation of validation functions.</p>