Search code examples
pythonpython-3.xtypenamenamedtuple

Relevance of typename in namedtuple


from collections import namedtuple

Point = namedtuple('whatsmypurpose',['x','y'])
p = Point(11,22)
print(p)

Output:

whatsmypurpose(x=11,y=22)

What's the relevance/use of 'whatsmypurpose'?


Solution

  • namedtuple() is a factory function for tuple subclasses. Here, 'whatsmypurpose'is the type name. When you create a named tuple, a class with this name (whatsmypurpose) gets created internally.

    You can notice this by using the verbose argument like:

    Point=namedtuple('whatsmypurpose',['x','y'], verbose=True)
    

    Also you can try type(p) to verify this.