Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3244204
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:31:23+00:00 2026-05-17T18:31:23+00:00

def foo(a, b, c = 0): return a+b I have dozens of functions like

  • 0
def foo(a, b, c = 0):
    return a+b

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?

Yes I can just generate a new function like the following:

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

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’.

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

Thanks!

Update
Finally this problem was solved using decorator module and the following patch:

--- /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(

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.

example 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.
>>> import decorator
>>> class A:
...   def f(self):
...       pass
... 
>>> a = A()
>>> a.f
<bound method A.f of <__main__.A instance at 0xb774a20c>>
>>> def hello(f, *args, **kw):
...     print 'hello'
...     return f(*args, **kw)
... 
>>> f1 = decorator.decorator(hello, a.f)
>>> f1()
hello
>>>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:31:24+00:00Added an answer on May 17, 2026 at 6:31 pm

    About your problem :

    “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’.”

    you can use the decorator module which enable you to create a signature-preserving decorators.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have two functions: def foo(): return 'foo' def bar(): yield 'bar'
If I have a function like the following: def foo(): return 1, 2 I
Suppose I have a function like: def foo(): x = 'hello world' How do
Have a method with the following signature: def foo(self, bar, *uks): return other_method(..., uks)
I have the following code: def foo(*args) print len(args) print args now I'd like
Suppose I have a function, say: >>> def foo(a): return a+1 I want to
Suppose we have the following class hierarchy: class ClassA: @property def foo(self): return hello
I have written a Python module which contains functions that return arrays. I want
I have a script a.py : #!/usr/bin/env python def foo(arg1, arg2): return int(arg1) +
def foo(a): a.append(1) if len(a) > 10: print a return a else: foo(a) Why

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.