如何记录分配的而不是定义的功能?

How to document functions that are assigned, rather than defined?

在 Python 中使用函数式编程时,我发现自己使用赋值声明函数,例如

where = compose(pluck(0), filter(get(1)), enumerate)

而不是使用 def例如

def where(seq):
    """Return the indices of truthy values in a sequence."""
    return pluck(0, filter(get(1), enumerate(seq))

第一种方法优雅简洁,但是没有文档字符串,这是不可接受的。第二种方法允许使用文档字符串,但不太优雅,也不太符合函数式风格。

在 Python 中记录由赋值创建的函数的最佳实践是什么?

您可以在 Python 中记录此类函数;但是,这样做并不是好的做法。不幸的是,尽管 def fun 至少现在看起来并不符合函数式风格,但这似乎是最佳实践。解决方法如下所示:

where = compose(pluck(0), filter(get(1)), enumerate)
where.__doc__ = """some docstrings here"""