title: How can I add keyword arguments to a wrapped function in Python 2.7?
body: <p>I first want to stress that I have searched both the web generally and the Python documentation + StackOverflow specifically very extensively and did not manage to find an answer to this question. I also want to thank anyone taking the time to read this.</p>
  
  <p>As the title suggests, I am writing a decorator in Python, and I want it to add keyword arguments to the wrapped function (<strong>please note</strong>: I know how to add arguments to the decorator itself, that's not what I'm asking).</p>
  
  <p>Here is a <strong>working example</strong> of a piece of code I wrote that does exactly that for Python 3 (specifically Python 3.5). It uses decorator arguments, adds keyword arguments to the wrapped function and also defines and adds a new function to the wrapped function.</p>
  
  <pre><code>from functools import wraps
  
  def my_decorator(decorator_arg1=None, decorator_arg2=False):
      # Inside the wrapper maker
  
      def _decorator(func):
          # Do Something 1
  
          @wraps(func)
          def func_wrapper(
                  *args,
                  new_arg1=False,
                  new_arg2=None,
                  **kwds):
              # Inside the wrapping function
              # Calling the wrapped function
              if new_arg1:
                  return func(*args, **kwds)
              else:
                  # do something with new_arg2
                  return func(*args, **kwds)
  
          def added_function():
              print("Do Something 2")
  
          func_wrapper.added_function = added_function
          return func_wrapper
  
      return _decorator
  </code></pre>
  
  <p>Now this decorator can be used in the following manner:</p>
  
  <pre><code>@my_decorator(decorator_arg1=4, decorator_arg2=True)
  def foo(a, b):
      print("a={}, b={}".format(a,b))
  
  def bar():
      foo(a=1, b=2, new_arg1=True, new_arg2=7)
      foo.added_function()
  </code></pre>
  
  <p>Now, while this works for Python 3.5 (and I assume for any 3.x), I have not managed to make it work for Python 2.7. I'm getting a <code>SyntaxError: invalid syntax</code> on the first line that tries to define a new keyword argument for the <code>func_wrapper</code>, meaning the line stating <code>new_arg1=False,</code>, when importing the module containing this code.</p>
  
  <p>Moving the new keywords to the start of the argument list of <code>func_wrapper</code> solves the <code>SyntaxError</code> but seems to screw with the wrapped function's signature; I'm now getting the error <code>TypeError: foo() takes exactly 2 arguments (0 given)</code> when calling <code>foo(1, 2)</code>. This error disappears if I assign the arguments explicitly, as in <code>foo(a=1, b=2)</code>, but that is obviously not enough - unsurprisingly, my new keyword arguments seem to be "stealing" the first two positional arguments sent to the wrapped function. This is something that did not happen with Python 3.</p>
  
  <p>I would love to get your help on this. Thank you for taking the time to read this.</p>
  
  <p>Shay</p>