Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about python __doc__ docstring

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?

like image 529
mlzboy Avatar asked Dec 09 '22 13:12

mlzboy


2 Answers

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
like image 56
AndiDog Avatar answered Dec 21 '22 10:12

AndiDog


Use functools.wraps().

like image 34
Ignacio Vazquez-Abrams Avatar answered Dec 21 '22 09:12

Ignacio Vazquez-Abrams