<p>是否可以以简单的方式将文档字符串添加到namedtuple?</p>
<p>我试过了</p>
<pre><code>from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
"""
A point in 2D space
"""
# Yet another test
"""
A(nother) point in 2D space
"""
Point2 = namedtuple("Point2", ["x", "y"])
print Point.__doc__ # -> "Point(x, y)"
print Point2.__doc__ # -> "Point2(x, y)"
</code></pre>
<p>但这并不能切断它。有别的办法吗?</p>
<p>在想同样的事情的时候,通过谷歌遇到了这个老问题。</p>
<p>只是想指出,您可以通过从类声明中调用namedtuple()来整理它:</p>
<pre><code>from collections import namedtuple
class Point(namedtuple('Point', 'x y')):
"""Here is the docstring."""
</code></pre>