3

I get function parameters list in this way:

def foo(a, b, c):
    # do something
    return

print inspect.getargspec(foo)

Output is:

ArgSpec(args=['a', 'b', 'c'], varargs=None, keywords=None, defaults=None)

But if the target function has a decorator, the results will be changed:

def dec_func(func):
    def wrapper(*args):
        # do something
        return func(*args)
    return wrapper

@dec_func
def foo(a, b, c):
    # do something
    return

print inspect.getargspec(foo)

Now the output is:

ArgSpec(args=[], varargs='args', keywords=None, defaults=None)

We can see, the info is about the wrapper in dec_func, not foo I want.

Any idea about how to still get info of foo?

Btw, why I ask it: Suppose I have a function A, and in some other codes I get the parameter list of function A to check/do something. This system will be broken if I add decorators to A, e.g. @numba.jit or @profile...

The solution is found: https://hynek.me/articles/decorators/

from decorator import decorator

@decorator
def dec_func(func):
    def wrapper(*args):
        # do something
        return func(*args)
    return wrapper

@dec_func
def foo(a, b, c):
    # do something
    return

print inspect.getargspec(foo)

The output is:

ArgSpec(args=['a', 'b', 'c'], varargs=None, keywords=None, defaults=None)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rocky.L
  • 59
  • 4

0 Answers0