Search code examples
pythonpython-3.xdata-structuresnamedtuple

Why does namedtuple in python need a name?


Why do we need to mention the name Card for namedtuple like below?

import collections

Card = collections.namedtuple('Card', ['rank', 'suit'])

I think a simple Card = collections.namedtuple(['rank', 'suit']) can actually give the same effect right?

For example I can have the same information in a dict like:

card = dict({'rank': 'A', 'suit': 'spade'})

Solution

  • No, that won't give the same effect.

    collections.namedtuple: Returns a new tuple subclass named typename...

    namedtuple returns a subclass of the tuple type, not a tuple instance.

    The name parameter specifies the class name of the new subclass, just as you would define a regular Python class and give it a name:

    >>> from collections import namedtuple
    >>> namedtuple('Card', ['rank', 'suit'], verbose=True)
    class Card(tuple):
        'Card(rank, suit)'
    
        __slots__ = ()
    
        _fields = ('rank', 'suit')
    
        def __new__(_cls, rank, suit):
            'Create new instance of Card(rank, suit)'
            return _tuple.__new__(_cls, (rank, suit))
        ...
    

    A quick type check clears every doubt:

    >>> type(_), issubclass(_, tuple)
    (<class 'type'>, True) 
    

    So there, you have namedtuple, a factory function that returns a subclass of a tuple.