您可以通过围绕返回的值创建一个简单的空包装类来实现此目的
namedtuple。我创建的文件的内容(
nt.py):
from collections import namedtuplePoint_ = namedtuple("Point", ["x", "y"])class Point(Point_): """ A point in 2d space """ pass然后在Python REPL中:
>>> print nt.Point.__doc__ A point in 2d space
或者您可以这样做:
>>> help(nt.Point) # which outputs...类nt模块nt中的帮助:类Point(Point) | 二维空间中的一点 | | 方法解析顺序: | 点 | 点 | __内置__。元组 | __内置__。对象 ...
如果您不喜欢每次都手动执行此操作,则编写一个排序工厂函数来执行此操作很简单:
def NamedTupleWithDocstring(docstring, *ntargs): nt = namedtuple(*ntargs) class NT(nt): __doc__ = docstring return NTPoint3D = NamedTupleWithDocstring("A point in 3d space", "Point3d", ["x", "y", "z"])p3 = Point3D(1,2,3)print p3.__doc__输出:
A point in 3d space



