如何解决在文档中手动定义函数签名
我正在编写一个我不一定知道其参数的函数。如何在方法定义中没有硬编码参数的情况下声明文档中显示的参数?
具体来说,我使用 **kwargs
来弥补不知道输入的问题:
# mymodule.py
class Wrapper():
def wrapped_method(self,**kwargs):
known = kwargs.get('known')
print(f'I know that {known} is needed,so I check for it explicitly.')
print('Anything else gets ignored. This allows me to cope with unknown parameters.')
print('It allows me to easily add/remove parameters.')
这很好用,除了文档阅读器(例如 Pycharm 的视图 > 快速文档或 Elpy 的 elpy-doc)返回:
Documentation for mymodule.Wrapper.wrapped_method:
wrapped_method(self,**kwargs)
能够在签名中显示 known
而不是无意义的 **kwargs
会很好。当然我可以在 docstring 中写出哪些 kwargs 被接受,但我希望我可以更直接地与用户交流。
这是我的意思的一个例子。在 Emacs Lisp 中,您可以使用 \fn(...)
手动定义文档中显示的签名:
(defun my-func (msg &rest args)
"Display MSG.
\(fn MSG)"
;; ARGS is an internal parameter the user doesn't need to care
;; about. It can be hidden from the user by declaring the signature
;; displayed within the help system with \\(fn MSG)
(message "%s" msg))
文档显示:
my-func is a Lisp function.
(my-func MSG)
Display MSG.
也许我可以用 Python 做类似的事情?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。