Is it possible to modify the return value of a function without defining new function in python?
<pre><code>def foo(a, b, c = 0):
    return a+b
</code></pre>

<p>I have dozens of functions like 'foo', which all have different argument numbers and names. Is there a common way that I can get the return values of these functions and do just a single extra operation like pformat to them?</p>

<p>Yes I can just generate a new function like the following:</p>

<pre><code>func = ...  # func can be got using getattr by name
def wrapper(*arg, **kw):
    data = func(*arg, **kw)
    return pprint.pformat(data)
return wrapper
</code></pre>

<p>But then the new function 'wrapper' is different to the old one 'func', for example, in argument number, 'wrapper' has only 2 args--'arg' and 'kw', but 'func' may have many args, like 'a', 'b', 'c'.</p>

<p>I just want play with the return value, everything else should stay still, is it possible?</p>

<p>Thanks!</p>

<p><strong>Update</strong>
Finally this problem was solved using <a href="http://pypi.python.org/pypi/decorator/3.2.0" rel="nofollow">decorator</a> module and the following patch:</p>

<pre><code>--- /home/jaime/cache/decorator-3.2.0/src/decorator.py  2010-05-22 23:53:46.000000000 +0800
+++ decorator.py    2010-10-28 14:55:11.511140589 +0800
@@ -66,9 +66,12 @@
             self.name = '_lambda_' 
             self.doc = func.__doc__
             self.module = func.__module__
-            if inspect.isfunction(func):
+            if inspect.isfunction(func) or inspect.ismethod(func):
                 argspec = inspect.getargspec(func)
                 self.args, self.varargs, self.keywords, self.defaults = argspec
+                if inspect.ismethod(func):
+                    self.args = self.args[1:] # Remove the useless 'self' arg
+                    argspec = inspect.ArgSpec(self.args, self.varargs, self.keywords, self.defaults)
                 for i, arg in enumerate(self.args):
                     setattr(self, 'arg%d' % i, arg)
                 self.signature = inspect.formatargspec(
</code></pre>

<p>This patch allows you to decorate bounded methods, it just throws the first 'self' argument away, usage of decorator.decorator stays the same, no bad effects found right now.</p>

<p>example code:</p>

<pre><code>def __getattr__(self, attr):
    def pformat_wrapper(f, *args, **kw):
        data = f(*args, **kw)
        return pprint.pformat(data, indent = 4)

    method = getattr(self.ncapi, attr)
    return decorator(pformat_wrapper, method) # Signature preserving decorating





jaime@westeros:~/bay/dragon.testing/tests$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import decorator
&gt;&gt;&gt; class A:
...   def f(self):
...       pass
... 
&gt;&gt;&gt; a = A()
&gt;&gt;&gt; a.f
&lt;bound method A.f of &lt;__main__.A instance at 0xb774a20c&gt;&gt;
&gt;&gt;&gt; def hello(f, *args, **kw):
...     print 'hello'
...     return f(*args, **kw)
... 
&gt;&gt;&gt; f1 = decorator.decorator(hello, a.f)
&gt;&gt;&gt; f1()
hello
&gt;&gt;&gt;
</code></pre>
