开发者

Can pydoc/help() hide the documentation for inherited class methods and attributes?

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 mo开发者_Go百科dule 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).


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜