簡體   English   中英

如何使用 pipetools 為以函數范式編寫的函數編寫文檔字符串

[英]How to write docstrings for functions written in functional paradigm using pipetools

我使用pipetools來編寫函數,它是一個“python 的函數式管道庫”。

一個示例函數(來自他們的文檔):

pyfiles_by_length = (pipe
    | os.listdir
    | where(X.endswith('.py'))
    | sort_by(len).descending
    | (enumerate, X, 1)
    | foreach("{0}. {1}")
    | '\n'.join)

這是一個函數定義。 我們將如何為這樣的函數編寫文檔字符串

我嘗試過的:

  1. 函數前注釋(PRO:干凈簡單。CON:不適用於文檔庫)
# print python filenames in descending order by length
pyfiles_by_length = (pipe
    | os.listdir
    | where(X.endswith('.py'))
    | sort_by(len).descending
    | (enumerate, X, 1)
    | foreach("{0}. {1}")
    | '\n'.join)
  1. __doc__ (PRO:使用文檔庫,CON:不干凈和簡單)
pyfiles_by_length = (pipe
    | os.listdir
    | where(X.endswith('.py'))
    | sort_by(len).descending
    | (enumerate, X, 1)
    | foreach("{0}. {1}")
    | '\n'.join)
pyfiles_by_length.__doc__ = 'print python filenames in descending order by length'

如果您需要文檔字符串,我建議您使用標准的 Python 方式:

def pyfiles_by_length(directory):
    """
    Returns all Python files in `directory` sorted by length
    """
    return (directory > pipe
        | os.listdir
        | where(X.endswith('.py'))
        | sort_by(len).descending
        | (enumerate, X, 1)
        | foreach("{0}. {1}")
        | '\n'.join)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM