pydoc/help() 可以隐藏继承类方法和属性的文档吗?
声明从特定类继承的类时:
class C(dict):
added_attribute = 0
类 C
的文档列出了 dict
的所有方法(通过 help(C)
或 pydoc)。
有没有办法从自动生成的文档中隐藏继承的方法(对于非覆盖方法,文档字符串可以引用基类)?或者这是不可能的?
这很有用:pydoc
列出了模块中定义的函数在其类之后。因此,当类有很长的文档时,在提供模块提供的新函数之前会打印很多不太有用的信息,这使得文档更难利用(您必须跳过继承方法的所有文档)直到您到达正在记录的模块的特定内容)。
When declaring a class that inherits from a specific class:
class C(dict):
added_attribute = 0
the documentation for class C
lists all the methods of dict
(either through help(C)
or pydoc
).
Is there a way to hide the inherited methods from the automatically generated documentation (the documentation string can refer to the base class, for non-overwritten methods)? or is it impossible?
This would be useful: pydoc
lists the functions defined in a module after its classes. Thus, when the classes have a very long documentation, a lot of less than useful information is printed before the new functions provided by the module are presented, which makes the documentation harder to exploit (you have to skip all the documentation for the inherited methods until you reach something specific to the module being documented).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,并在 Python 2.7.6 for Windows (x86) 上通过向 pydoc.py 添加 3 行来解决它。说明:
查找文件中所有出现的
inherited
变量(据我所知,有 3 次),然后 >定义后立即将其设置为空列表。例如,我得到第 809 行:attrs,herited = _split_list(attrs, lambda t: t[2] is thisclass)
并在其下面的新行中写入
inherited = []
。现在它不再打印继承的方法。
I had the same problem and solved it on Python 2.7.6 for Windows (x86) by adding 3 lines to pydoc.py. Instructions:
find all occurences of the
inherited
variable (3 times, on my count) in the file and set it to an empty list right after it is defined. For example, I got line 809:attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
and wrote
inherited = []
on a new line below it.Now it doesn't print inherited methods anymore.
pydoc
和help
内置函数不支持这一点,但是您没有理由不能编写自己的工具(也许通过修改 pydoc 的源代码)来实现您想要的行为欲望。只需遍历类的 dict 即可获取本地定义的属性,然后查找具有 doc 作为属性的内容。pydoc
and thehelp
builtin don't support this, but there's no reason you couldn't write your own tool (perhaps by modifying pydoc's source) that would have the behavior you desire. Just walk the dict of classes to get the locally-defined attributes, then look for things that have doc as an attribute.您可以使用特殊方法 __dir__ 为您的类提供一个元类,该方法返回其自己的属性列表。 Pydoc 将使用这个列表。
注意:
You can give your class a metaclass with the special method
__dir__
that returns its own list of attributes. Pydoc will use this list.Notes:
dir()
.