Namedtuples

Out of the handy collections module, I’m using namedtuple datatype the most.

Once instantiated, namedtuples can be used like regular tuples:

>>> from collections import namedtuple

>>> Dog = namedtuple('Dog', 'age breed color')
>>> rachel = Dog(2, 'Pomeranian', 'Brown')

>>> rachel[0]
2

>>> name, breed, color = rachel
>>> color
'Brown'

With the advantage of accessing the fields by attribute (name) lookup:

>>> rachel.breed
'Pomeranian'

And an awesome string __repr__() with a name=value style:

>>> rachel
Dog(age=2, breed='Pomeranian', color='Brown')

Main Use cases

I found namedtuple especially useful when:

  • Grouping multiple values, in a shorter way than manually defining a class
  • When a function returns several values (say more than 3) returning a namedtuple makes it easier to unpack them

Pomeranian

Tips & tricks

  • To view the fields names use _fields method

  • Tuples are immutable, therefore you’ll need to use the _replace method to change a value:

>>> rachel.color = 'Tan'
AttributeError: "can't set attribute"

>>> rachel._replace(color='Tan')
Dog(age=2, breed='Pomeranian', color='Tan')
  • To convert a dictionary to a named tuple:
>>> pepsi = {'age': 12, 'breed': 'Belgian Shepherd', 'color': 'Black'}
>>> Dog(**dict)
Dog(age=12, breed='Belgian Shepherd', color='Black')
comments powered by Disqus