Stephen Lu
Stephen Lu

Reputation: 314

Prepending a function docstring with a decorator in python

How would you be able to prepend a functions docstring with a decorator?

def g(func):
    someOtherDocString = "That is great"
    def wrap(*args, **kwargs):
        func(*args, **kwargs)
return wrap

@g
def f():
""" This is awesome """

result:

>>>help(f)

Help on function f in module __main__:

f()
    That is great
    That is awesome

All the help would be greatly appreciated.

Upvotes: 2

Views: 264

Answers (1)

Artsiom Rudzenka
Artsiom Rudzenka

Reputation: 29103

Have you tried magic __doc__:

from functools import wraps

def g(func):
    func.__doc__ = "That is great" + func.__doc__

    @wraps(func)
    def wrap(*args, **kwargs):
        return func(*args, **kwargs)

    return wrap

Upvotes: 6

Related Questions