pydoc/help() 可以隐藏继承类方法和属性的文档吗?

发布于 2024-09-01 19:37:01 字数 375 浏览 10 评论 0原文

声明从特定类继承的类时:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅忆 2024-09-08 19:37:01

我遇到了同样的问题,并在 Python 2.7.6 for Windows (x86) 上通过向 pydoc.py 添加 3 行来解决它。说明:

  1. 制作您自己的 Lib\pydoc.py 副本
  2. 查找文件中所有出现的 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:

  1. make your own copy of Lib\pydoc.py
  2. 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.

但可醉心 2024-09-08 19:37:01

pydochelp 内置函数不支持这一点,但是您没有理由不能编写自己的工具(也许通过修改 pydoc 的源代码)来实现您想要的行为欲望。只需遍历类的 dict 即可获取本地定义的属性,然后查找具有 doc 作为属性的内容。

pydoc and the help 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.

简美 2024-09-08 19:37:01

您可以使用特殊方法 __dir__ 为您的类提供一个元类,该方法返回其自己的属性列表。 Pydoc 将使用这个列表。

注意:

  • 这也会影响 dir() 的行为。
  • 在极少数情况下,使用元类会打开通往地狱维度的门户。

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:

  • this will also affect the behaviour of dir().
  • in rare cases, use of metaclasses has been known to open a portal to a hell dimension.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文