Duplicate Docs

What are some good ways to deal with repetitive content in docstrings? I have many functions that take “standard” arguments, which should be explained in docstring, but it would be nice to write the corresponding parts of docstring only once, as that would be much easier to maintain and update. I naively tried the following:

arg_a = "a: a very common argument"

def test(a):
    '''
    Arguments:
    %s
    ''' % arg_a
    pass

But this does not work, because when I do help(test), I do not see a docstring. Is there a good way to do this?

+3
source share
3 answers

, __doc__ . - , docstring:

def fixdocstring(func):
    func.__doc__ = func.__doc__.replace('<arg_a>', 'a: a very common argument')
    #(This is just an example, other string formatting methods can be used as well.)
    return func

@fixdocstring
def test(a):
    '''
    Arguments:
    <arg_a>
    ''''
    pass
+4

__doc__ :

arg_a = "a: a very common argument"

def test(a):
    pass

test.__doc__ = '''
    Arguments:
    %s
    ''' % arg_a
+3

, ( , __doc__, ).

, . :

, docstring 300- ? , ?

+3

Source: https://habr.com/ru/post/1739785/


All Articles