首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用变量定义多个函数docstring

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

Stack Overflow用户
提问于 2017-05-25 06:38:13
回答 1查看 666关注 0票数 4

我有几个函数(abc),我希望它们使用相同的docstring。因此,我的计划是保存行,只编写一次docstring,并将其保存到变量DOCSTRING中。然后我把它放在函数声明下。我在PEP 257上没有发现任何能解决我的问题的东西.

代码语言:javascript
复制
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)

实际上我想这可能是work...but我错了,我明白了:

代码语言:javascript
复制
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

代码语言:javascript
复制
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)

这两种方法的输出结果与之前的尝试相同.

目前起作用的是好的复制粘贴方法:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
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

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

所以,现在,我的问题是,如果我将docstring复制到每个函数,,而不需要将它复制到每个函数,那么如何才能得到相同的结果呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-25 06:47:25

您的问题是,尝试在函数体中设置docstring将无法工作,因为除非函数实际被调用,否则这些行永远不会被计算。你需要的是类似(或相当于)的东西:

代码语言:javascript
复制
def c(x, y):
    # code

c.__doc__ = DOCSTRING
help(c)

你会想要用一个装饰师。

代码语言:javascript
复制
def setdoc(func):                                                                                                                                                                                                            
    func.__doc__ = DOCSTRING                                                                                                                                                                                                 
    return func

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

help(c) #Output docstring
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44173889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档