Subclassing collections namedtuple

Viewed 17433

Python's namedtuple can be really useful as a lightweight, immutable data class. I like using them for bookkeeping parameters rather than dictionaries. When some more functionality is desired, such as a simple docstring or default values, you can easily refactor the namedtuple to a class. However, I've seen classes that inherit from namedtuple. What functionality are they gaining, and what performance are they losing? For example, I would implement this as

from collections import namedtuple

class Pokemon(namedtuple('Pokemon', 'name type level')):
    """
    Attributes
    ----------
    name : str
        What do you call your Pokemon?
    type : str
        grass, rock, electric, etc.
    level : int
        Experience level [0, 100]
    """
     __slots__ = ()

For the sole purpose of being able to document the attrs cleanly, and __slots__ is used to prevent the creation of a __dict__ (keeping the lightweight nature of namedtuples).

Is there a better recommendation of a lightweight data class for documenting parameters? Note I'm using Python 2.7.

1 Answers
Related