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

abc*_*ccd 3 python docstring

我有几个函数(a,bc),我希望它们使用相同的文档字符串。所以我的计划是通过只写一次文档字符串并将其保存到变量来保存行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)
Run Code Online (Sandbox Code Playgroud)

我实际上认为它可能有效......但我错了,我得到了这个:

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)
Run Code Online (Sandbox Code Playgroud)

它根本没有用。


我进行了第二次尝试,__doc__将函数的特殊属性更改为我的 docstring 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)
Run Code Online (Sandbox Code Playgroud)

并且这两种方法都获得了与上一次尝试相同的输出......


目前有效的是 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)
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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


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

Ult*_*nct 6

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

def c(x, y):
    # code

c.__doc__ = DOCSTRING
help(c)
Run Code Online (Sandbox Code Playgroud)

你会想要使用装饰器。

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

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

help(c) #Output docstring
Run Code Online (Sandbox Code Playgroud)