Adding docstrings to namedtuples?

前端 未结 10 1542
死守一世寂寞
死守一世寂寞 2020-12-08 01:52

Is it possible to add a documentation string to a namedtuple in an easy manner?

I tried

from collections import namedtuple

Point = namedtuple(\"Poin         


        
10条回答
  •  长情又很酷
    2020-12-08 02:20

    No need to use a wrapper class as suggested by the accepted answer. Simply literally add a docstring:

    from collections import namedtuple
    
    Point = namedtuple("Point", ["x", "y"])
    Point.__doc__="A point in 2D space"
    

    This results in: (example using ipython3):

    In [1]: Point?
    Type:       type
    String Form:
    Docstring:  A point in 2D space
    
    In [2]: 
    

    Voilà!

提交回复
热议问题