将文档字符串添加到namedtuples?


85

是否可以通过简单的方式将文档字符串添加到namedtuple?

我试过了

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)"

但这并不能解决问题。是否可以通过其他方式进行?

Answers:


53

您可以通过围绕返回的值创建一个简单的空包装类来实现此目的namedtuple。我创建的文件的内容(nt.py):

from collections import namedtuple

Point_ = 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 NT

Point3D = NamedTupleWithDocstring("A point in 3d space", "Point3d", ["x", "y", "z"])

p3 = Point3D(1,2,3)

print p3.__doc__

输出:

A point in 3d space

2
子类化不会将转换namedtuple为完整的“对象”吗?从而损失了命名元组的一些性能提升?
exhuma '17

5
如果添加__slots__ = ()到派生子类,则可以保留使用namedtuple
–ali_m

它仍然为MRO添加了另一个级别,这对于文档字符串是不合理的。但是,您可以简单地分配给__doc__原始对象并在其中保存自定义文档字符串。
巴查(Bachsau)

70

在Python 3中,不需要包装,因为__doc__类型的属性是可写的。

from collections import namedtuple

Point = namedtuple('Point', 'x y')
Point.__doc__ = '''\
A 2-dimensional coordinate

x - the abscissa
y - the ordinate'''

这与标准类定义非常相似,其中文档字符串位于标头之后。

class Point():
    '''A 2-dimensional coordinate

    x - the abscissa
    y - the ordinate'''
    <class code>

这在Python 2中不起作用。

AttributeError: attribute '__doc__' of 'type' objects is not writable


64

通过Google遇到了这个老问题,同时也想知道同样的事情。

只是想指出,您可以通过直接从类声明中调用namedtuple()来整理它:

from collections import namedtuple

class Point(namedtuple('Point', 'x y')):
    """Here is the docstring."""

8
重要的是您要包括__slots__ = ()在课程中。否则,您__dict__将为attrs创建一个,从而失去namedtuple的轻量级特性。
BoltzmannBrain

34

是否可以通过简单的方式将文档字符串添加到namedtuple?

是的,有几种方法。

子类的输入NamedTuple-Python 3.6+

对于Python 3.6的,我们可以用一个class定义与typing.NamedTuple直接与文档字符串(和注解!):

from typing import NamedTuple

class Card(NamedTuple):
    """This is a card type."""
    suit: str
    rank: str

与Python 2相比,__slots__不需要声明为空。在Python 3.8中,即使对于子类也没有必要。

请注意,声明__slots__不能为空!

在Python 3中,您还可以轻松地更改namedtuple上的文档:

NT = collections.namedtuple('NT', 'foo bar')

NT.__doc__ = """:param str foo: foo name
:param list bar: List of bars to bar"""

当我们向他们求助时,这使我们可以查看他们的意图:

Help on class NT in module __main__:

class NT(builtins.tuple)
 |  :param str foo: foo name
 |  :param list bar: List of bars to bar
...

与我们在Python 2中完成同一件事所遇到的困难相比,这确实很简单。

Python 2

在Python 2中,您需要

  • 将namedtuple子类化,并且
  • 宣布 __slots__ == ()

声明__slots__此处其他答案缺少的重要部分

如果不声明__slots__,则可以向实例添加可变的临时属性,从而引入错误。

class Foo(namedtuple('Foo', 'bar')):
    """no __slots__ = ()!!!"""

现在:

>>> f = Foo('bar')
>>> f.bar
'bar'
>>> f.baz = 'what?'
>>> f.__dict__
{'baz': 'what?'}

每个实例__dict____dict__访问时都会创建一个单独的实例(缺少__slots__不会阻止其功能,但是元组的轻巧性,不变性和声明的属性都是namedtuple的重要特征)。

__repr__如果您希望在命令行上回显所提供的等效对象,则还需要一个:

NTBase = collections.namedtuple('NTBase', 'foo bar')

class NT(NTBase):
    """
    Individual foo bar, a namedtuple

    :param str foo: foo name
    :param list bar: List of bars to bar
    """
    __slots__ = ()

__repr__,如果你创建一个不同的名称基本namedtuple(就像我们的名称字符串参数,上面不喜欢,这是需要'NTBase'):

    def __repr__(self):
        return 'NT(foo={0}, bar={1})'.format(
                repr(self.foo), repr(self.bar))

要测试代表,请实例化,然后测试是否与 eval(repr(instance))

nt = NT('foo', 'bar')
assert eval(repr(nt)) == nt

文档中的示例

文档还提供了一个有关以下示例:__slots__-我在其中添加了自己的文档字符串:

class Point(namedtuple('Point', 'x y')):
    """Docstring added here, not in original"""
    __slots__ = ()
    @property
    def hypot(self):
        return (self.x ** 2 + self.y ** 2) ** 0.5
    def __str__(self):
        return 'Point: x=%6.3f  y=%6.3f  hypot=%6.3f' % (self.x, self.y, self.hypot)

...

上面显示的子类设置__slots__为一个空元组。通过防止创建实例字典,这有助于将内存需求保持在较低水平。

这演示了就地用法(如此处的另一个答案所示),但是请注意,如果您要调试,则在查看方法解析顺序时,就地用法可能会造成混淆,这就是我最初建议使用Base后缀的原因对于基本的namedtuple:

>>> Point.mro()
[<class '__main__.Point'>, <class '__main__.Point'>, <type 'tuple'>, <type 'object'>]
                # ^^^^^---------------------^^^^^-- same names!        

为了防止__dict__从使用它的类创建子类时创建a ,您还必须在子类中声明它。有关使用的更多注意事项,__slots__另请参阅此答案


3
尽管没有其他答案那么简洁明了,但这应该是公认的答案,因为它突出了的重要性__slots__。没有它,您将失去namedtuple的轻量级价值。
BoltzmannBrain

7

从Python 3.5开始,namedtuple可以更新对象的文档字符串。

whatsnew

Point = namedtuple('Point', ['x', 'y'])
Point.__doc__ += ': Cartesian coodinate'
Point.x.__doc__ = 'abscissa'
Point.y.__doc__ = 'ordinate'


3

无需使用已接受答案所建议的包装器类。只需从字面上添加一个文档字符串:

from collections import namedtuple

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

结果是:(使用的示例ipython3):

In [1]: Point?
Type:       type
String Form:<class '__main__.Point'>
Docstring:  A point in 2D space

In [2]: 

瞧!


1
注意:这仅对Python 3有效。在Python 2中:AttributeError: attribute '__doc__' of 'type' objects is not writable
泰勒·埃德米斯顿

1

您可以由Raymond Hettinger编写自己的namedtuple工厂函数版本,并添加一个可选docstring参数。但是,使用与配方中相同的基本技术来定义自己的工厂功能会更容易(可能会更好)。无论哪种方式,您最终都会得到可重用的东西。

from collections import namedtuple

def my_namedtuple(typename, field_names, verbose=False,
                 rename=False, docstring=''):
    '''Returns a new subclass of namedtuple with the supplied
       docstring appended to the default one.

    >>> Point = my_namedtuple('Point', 'x, y', docstring='A point in 2D space')
    >>> print Point.__doc__
    Point(x, y):  A point in 2D space
    '''
    # create a base class and concatenate its docstring and the one passed
    _base = namedtuple(typename, field_names, verbose, rename)
    _docstring = ''.join([_base.__doc__, ':  ', docstring])

    # fill in template to create a no-op subclass with the combined docstring
    template = '''class subclass(_base):
        %(_docstring)r
        pass\n''' % locals()

    # execute code string in a temporary namespace
    namespace = dict(_base=_base, _docstring=_docstring)
    try:
        exec template in namespace
    except SyntaxError, e:
        raise SyntaxError(e.message + ':\n' + template)

    return namespace['subclass']  # subclass object created

0

我创建此函数是为了快速创建一个命名的元组,并记录元组及其每个参数:

from collections import namedtuple


def named_tuple(name, description='', **kwargs):
    """
    A named tuple with docstring documentation of each of its parameters
    :param str name: The named tuple's name
    :param str description: The named tuple's description
    :param kwargs: This named tuple's parameters' data with two different ways to describe said parameters. Format:
        <pre>{
            str: ( # The parameter's name
                str, # The parameter's type
                str # The parameter's description
            ),
            str: str, # The parameter's name: the parameter's description
            ... # Any other parameters
        }</pre>
    :return: collections.namedtuple
    """
    parameter_names = list(kwargs.keys())

    result = namedtuple(name, ' '.join(parameter_names))

    # If there are any parameters provided (such that this is not an empty named tuple)
    if len(parameter_names):
        # Add line spacing before describing this named tuple's parameters
        if description is not '':
            description += "\n"

        # Go through each parameter provided and add it to the named tuple's docstring description
        for parameter_name in parameter_names:
            parameter_data = kwargs[parameter_name]

            # Determine whether parameter type is included along with the description or
            # if only a description was provided
            parameter_type = ''
            if isinstance(parameter_data, str):
                parameter_description = parameter_data
            else:
                parameter_type, parameter_description = parameter_data

            description += "\n:param {type}{name}: {description}".format(
                type=parameter_type + ' ' if parameter_type else '',
                name=parameter_name,
                description=parameter_description
            )

            # Change the docstring specific to this parameter
            getattr(result, parameter_name).__doc__ = parameter_description

    # Set the docstring description for the resulting named tuple
    result.__doc__ = description

    return result

然后,您可以创建一个新的命名元组:

MyTuple = named_tuple(
    "MyTuple",
    "My named tuple for x,y coordinates",
    x="The x value",
    y="The y value"
)

然后用您自己的数据实例化所描述的命名元组。

t = MyTuple(4, 8)
print(t) # prints: MyTuple(x=4, y=8)

help(MyTuple)通过python3命令行执行时,显示以下内容:

Help on class MyTuple:

class MyTuple(builtins.tuple)
 |  MyTuple(x, y)
 |
 |  My named tuple for x,y coordinates
 |
 |  :param x: The x value
 |  :param y: The y value
 |
 |  Method resolution order:
 |      MyTuple
 |      builtins.tuple
 |      builtins.object
 |
 |  Methods defined here:
 |
 |  __getnewargs__(self)
 |      Return self as a plain tuple.  Used by copy and pickle.
 |
 |  __repr__(self)
 |      Return a nicely formatted representation string
 |
 |  _asdict(self)
 |      Return a new OrderedDict which maps field names to their values.
 |
 |  _replace(_self, **kwds)
 |      Return a new MyTuple object replacing specified fields with new values
 |
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |
 |  _make(iterable) from builtins.type
 |      Make a new MyTuple object from a sequence or iterable
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(_cls, x, y)
 |      Create new instance of MyTuple(x, y)
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  x
 |      The x value
 |
 |  y
 |      The y value
 |
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  _fields = ('x', 'y')
 |  
 |  _fields_defaults = {}
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from builtins.tuple:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(self, key, /)
 |      Return self[key].
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __rmul__(self, value, /)
 |      Return value*self.
 |  
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |  
 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.
 |      
 |      Raises ValueError if the value is not present.

另外,您还可以通过以下方式指定参数的类型:

MyTuple = named_tuple(
    "MyTuple",
    "My named tuple for x,y coordinates",
    x=("int", "The x value"),
    y=("int", "The y value")
)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.