gpt4 book ai didi

python - 简化python中的类docstring

转载 作者:行者123 更新时间:2023-11-28 18:40:10 25 4
gpt4 key购买 nike

我有从 Exception 派生的最简单的类,看起来像这样:

class Test(Exception):
'''
My Test class
'''
def __init__(self, param=None):
self.param = param

def __str__(self):
return 'Test representation'

def foo(self):
'''Perform a foo'''
print 'Fubar'

当我在这个模块上运行 help() 时,我得到:

class Test(exceptions.Exception)
| My Test class
|
| Method resolution order:
| Test
| exceptions.Exception
| exceptions.BaseException
| __builtin__.object
|
| Methods defined here:
|
| __init__(self, param=None)
|
| __str__(self)
|
| foo(self)
| Perform a foo
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from exceptions.Exception:
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
|
| ----------------------------------------------------------------------
| Methods inherited from exceptions.BaseException:
|
| __delattr__(...)
| x.__delattr__('name') <==> del x.name
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __getslice__(...)
| x.__getslice__(i, j) <==> x[i:j]
|
| Use of negative indices is not supported.
|
| __reduce__(...)
|
| __repr__(...)
| x.__repr__() <==> repr(x)
|
| __setattr__(...)
| x.__setattr__('name', value) <==> x.name = value
|
| __setstate__(...)
|
| __unicode__(...)
|
| ----------------------------------------------------------------------
| Data descriptors inherited from exceptions.BaseException:
|
| __dict__
|
| args
|
| message

这是一个可怕的噪音,与类的文档无关。我该如何抑制所有这些?

[编辑]如果该类不是从 Exception 继承的,我希望它更像我得到的结果

像这样:

class Test
| My Test class
|
| Methods defined here:
|
| __init__(self, param=None)
|
| __str__(self)
|
| foo(self)
| Perform a foo

最佳答案

如果你输入:

help(help) 

你应该阅读:

Help on _Helper in module site object:

class _Helper(__builtin__.object)
| Define the builtin 'help'.
| This is a wrapper around pydoc.help (with a twist).
...

这意味着您应该能够编写您“自己的”辅助函数。看来您应该看一下 pydochelp() 实现。

关于python - 简化python中的类docstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27237072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com