Is it possible to add a documentation string to a namedtuple in an easy manner?
I tried
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)"
but that doesn’t cut it. Is it possible to do in some other way?
You can achieve this by creating a simple, empty wrapper class around the returned value from
namedtuple. Contents of a file I created (nt.py):Then in the Python REPL:
Or you could do:
If you don’t like doing that by hand every time, it’s trivial to write a sort-of factory function to do this:
which outputs: