使用一个变量定义多个函数docstring

Use a variable to define multiple functions docstring

我有几个函数(abc),我希望它们使用相同的文档字符串。所以我的计划是通过只编写一次文档字符串并将其保存到变量 DOCSTRING 来节省行数。然后我把它放在函数声明下。我没有在 PEP 257 中找到任何解决我问题的内容...

DOCSTRING = '''
This is a docstring
for functions:
a,
b,
c'''

def a(x, y):
    DOCSTRING
    # do stuff with x and y

def b(x, y):
    DOCSTRING
    # do other stuffs with x and y

def c(x, y):
    DOCSTRING
    # do some more stuffs with x and y

help(a), help(b), help(c)

我实际上认为它可能有用...但我错了,我明白了:

Help on function a in module __main__:

a(x, y)

Help on function b in module __main__:

b(x, y)

Help on function c in module __main__:

c(x, y)

一点用都没有。


我进行了第二次尝试,将函数的特殊 __doc__ 属性更改为我的文档字符串 DOCSTRING:

DOCSTRING = '''
This is a docstring
for functions:
a,
b,
c'''

def a(x, y):
    a.__doc__ = DOCSTRING # also tried just __doc__ = DOCSTRING, both didn't work
    # do stuff with x and y

def b(x, y):
    b.__doc__ = DOCSTRING # also tried just __doc__ = DOCSTRING, both didn't work
    # do other stuffs with x and y

def c(x, y):
    c.__doc__ = DOCSTRING # also tried just __doc__ = DOCSTRING, both didn't work
    # do some more stuffs with x and y

help(a), help(b), help(c)

这两种方法都得到了与之前尝试相同的输出...


目前有效的是 good'ole 复制粘贴方法:

def a(x, y):
    '''
    This is a docstring
    for functions:
    a,
    b,
    c'''

    # do stuff with x and y

def b(x, y):
    '''
    This is a docstring
    for functions:
    a,
    b,
    c'''

    # do other stuffs with x and y

def c(x, y):
    '''
    This is a docstring
    for functions:
    a,
    b,
    c'''

    # do some more stuffs with x and y

help(a), help(b), help(c)

当然,它会产生我想要的输出:

Help on function a in module __main__:

a(x, y)
    This is a docstring
    for functions:
    a,
    b,
    c

Help on function b in module __main__:

b(x, y)
    This is a docstring
    for functions:
    a,
    b,
    c

Help on function c in module __main__:

c(x, y)
    This is a docstring
    for functions:
    a,
    b,
    c

如你所见,但这种方式将迫使我不得不浪费多行来写同样的东西...


所以现在,我的问题是,如何才能获得与将文档字符串复制到每个函数相同的结果,而不必将其复制到每个函数?

您的问题是尝试在函数体内设置文档字符串是行不通的,因为除非实际调用函数,否则永远不会评估这些行。你需要的是类似(或等价物)的东西:

def c(x, y):
    # code

c.__doc__ = DOCSTRING
help(c)

您想使用装饰器。

def setdoc(func):                                                                                                                                                                                                            
    func.__doc__ = DOCSTRING                                                                                                                                                                                                 
    return func

@setdoc                                                                                                                                                                                                                
def c(x, y):                                                                                                                                                                                                                 
    print("hi")                                                                                                                                                                                                              

help(c) #Output docstring