Python docstring重复参数文本

Python docstring重复参数文本,python,docstring,Python,Docstring,在一个Python模块中(设计用于IPython笔记本电脑中的非技术用户),我有以下几个功能: load_this(dt, this_filter) load_that(dt, that_filter) load_the_other(dt, the_other_filter) dt参数的docstring对于每个函数都是相同的: :param dt: date, date tuple, or datetime tuple. date type args expand to start

在一个Python模块中(设计用于IPython笔记本电脑中的非技术用户),我有以下几个功能:

load_this(dt, this_filter)
load_that(dt, that_filter)
load_the_other(dt, the_other_filter)
dt参数的docstring对于每个函数都是相同的:

:param dt: date, date tuple, or datetime tuple. 
    date type args expand to start and end of day.
    eg.  date(2015, 9, 9) or
    (date(2015, 9, 9), date(2015, 9, 10)) or
    (datetime(2015, 9, 9, 12, 30), datetime(2015, 9, 9, 1))
但是,x_过滤器参数的记录在每种情况下都是不同的

我尽量在代码中保持干爽,所以重复的docstring有点刺耳。是否有任何方法可以在代码中交叉引用docstring参数,但仍然让IPython显示完整的docstring


谢谢

您可以通过decorator将文档添加到函数中

def mydoc(func):
    func.__doc__ = 'Here I am.'
    return func

@mydoc
def load_this(dt):
    pass
带有自定义文档的变体

def setdoc(docstring):
    def decor(func):
        func.__doc__ = docstring
        return func
    return decor

@setdoc('Here I am.')
def load_this(dt):
    pass
或者在定义函数后添加文档

docmessage = 'Here I am.'
def load_this(dt):
    pass
load_this.__doc__ = docmessage

与其以编程方式生成docstring(在我看来,这是一种非音速的方式),还不如定义一个函数
load
like

def load(dt, filter_):
    ... ...
并为其编写一份文档?我真的看不出需要3个独立的函数

即使您知道一些方法,您也可以在定义上述函数
load
后,通过它们创建它们


编辑:在OP的评论之后,我想你只需在一个函数(大概是最基本的“原型”函数)中为
dt
编写一次文档,然后在另一个函数中简单地说

"See the documentation of ___ for details about dt".
是的,您可以像这样巧妙地拼接docstring

some_function.__doc__ = some_function.__doc__ + "doc for dt"

或者更聪明地编写自己的文档生成元类生成器factor factory框架,但为什么?;)

谢谢,那很有用。。我应该在不同的函数中指定其他异构参数。。只有dt论点是常见的。但是,我想我可以用doc引用做一些字符串替换?我要玩它。(尽管我担心事情变得太神奇:)是的,你可以修改现有的文档而不是替换。默认情况下,“单据”为“无”。现在我再添加一个示例,将docstring作为参数传递给decoratora) 该代码是为非技术用户设计的。b) filter_uuargs是非常异构的,实际上并不是函数之间的唯一变体。有些函数有2个参数,有些函数有7个参数。