4

i want to show docstring of my function, but if i use like this

@cost_time
def func():
    "define ...."
    blabla
print func.__doc__

it will not show the docstring,just because i use some meta programming tricky, how can fix this?

mlzboy
  • 14,343
  • 23
  • 76
  • 97

2 Answers2

12

Your wrapped function returned from the cost_time decorator must have the docstring instead of func. Therefore, use functools.wraps which correctly sets __name__ and __doc__:

from functools import wraps

def cost_time(fn):
    @wraps(fn)
    def wrapper():
        return fn()

    return wrapper
AndiDog
  • 68,631
  • 21
  • 159
  • 205
2

Use functools.wraps().

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358