Use functools.wraps
.
Note that it doesn't fake out the argument specification. You probably could do that (using inspect.getargspec
to work out the argument specification of foo
and then hacking bar
), but it's not conventional.
EDIT: Since you're using Python 2.4, you'll have to re-implement the functionality of functools.wraps
. Fortunately it is actually pretty simple; it just copies over the attributes __module__
, __name__
and __doc__
, and then updates the wrapper's __dict__
with the wrapped function's __dict__
. So you could do:
WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__')
WRAPPER_UPDATES = ('__dict__',)
def update_wrapper(wrapper,
wrapped,
assigned = WRAPPER_ASSIGNMENTS,
updated = WRAPPER_UPDATES):
for attr in assigned:
setattr(wrapper, attr, getattr(wrapped, attr))
for attr in updated:
getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
# Return the wrapper so this can be used as a decorator via partial()
return wrapper
def wraps(wrapped,
assigned = WRAPPER_ASSIGNMENTS,
updated = WRAPPER_UPDATES):
return partial(update_wrapper, wrapped=wrapped,
assigned=assigned, updated=updated)
(FYI this is the actual source code of functools.wraps
.)