1

Quite related to about python __doc__ docstring. In case I don't use functools and use the wrapper as mentioned in How to print Docstring of python function from inside the function itself?, is there a way to get the docstring printed.

def passmein(func):
    def wrapper(*args, **kwargs):
        return func(func, *args, **kwargs)
    return wrapper

@passmein
def testfunc(me):
    """This is a test function"""
    #print me.__doc__

if __name__ == '__main__':
    print testfunc.__doc__

This returns none.

Community
  • 1
  • 1
  • 1
    Why are you trying to print `me.__doc__` instead of `testfunc.__doc__`? – abarnert Aug 09 '13 at 18:32
  • Meanwhile, if all you're asking is how to wrap functions in a way that preserves the docstring without using `functools.wraps`… just look at [the source](http://hg.python.org/cpython/file/2.7/Lib/functools.py#l15) and do the same thing. But I can't imagine why you'd ever want that. – abarnert Aug 09 '13 at 18:33
  • @abamert- The print was to use it to display the docstring from inside the function when I call it. – Satish Viswanathan Aug 10 '13 at 03:00
  • So why are you trying to print the docstring of its first parameter? – abarnert Aug 12 '13 at 18:18

1 Answers1

3

I'm not sure why you would not want to use functools.wraps, but you could add the doc string to wrapper yourself:

def passmein(func):
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    wrapper.__doc__ = func.__doc__
    return wrapper

@passmein
def testfunc(me):
    """This is a test function"""

if __name__ == '__main__':
    print testfunc.__doc__
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677