Very closely related to: How can I programmatically change the argspec of a function in a python decorator?
The decorator module provides the means to make a decorator function that preserves the argspec of the decorated function.
If I define a function that is not used as a decorator, is there a way to copy another function's argspec?
Example use case:
class Blah(object):
def foo(self, *args, **kwargs):
""" a docstr """
result = bar(*args, **kwargs)
result = result**2 # just so it's clear we're doing something extra here...
return result
def bar(x, y, z=1, q=2):
""" a more useful docstr, saying what x,y,z,q do """
return x+y*z+q
I would like to have foo
's argspec look like bar
's, but the source to stay unchanged (i.e., inspect.getsource(foo)
would still show the result
junk). The main purpose for this is to get sphinx docs and ipython's interactive help to show the appropriate arguments.
As the answers to the other question said, the decorator package shows a way to do this, but I got lost within the meat of that code. It seems that the decorator
package is recompiling the source, or something like that. I had hoped a simpler approach, e.g. something like foo.argspec = bar.argspec
, would be possible.