Create and Object With Namedtuple

Namedtuples are a subclass of typename which allow us to create objects (or classes) with tuple-like attributes, it’s easy to create a class with immutable attributes with this library.

Creating an object

To create a namedtuple we only need the name of the class first and then the attribute list.

>>> from collections import namedtuple
>>> Person = namedtuple('Person', 'name job_position sex married')
>>> john = Person('john', 'developer', 'male', False)
>>> john
Person(name='john', job_position='developer', sex='male', married=False)

Be aware that you can pass an argument list as a string (‘att1 att2’), as an actual list ([‘att1’, ‘att2’]) or a string with comma separated values (‘att1, att2’).

Accessing attributes

We can access the person attributes just like any other class.

>>> john.job_position
>>> john.married


We can also assign docstring to our attributes.

>>> Person.__doc__ = 'A person attributes'
'A person attributes'
>>> = "A person's name"
"A person's name"
>>> Person.job_position.__doc__ = "A person's job position"
"A person's job position"
>>> = 'Yes'
>>> Person.married.__doc__ = 'Is the person married?'
'Is the person married?'

Changing values

As our attribute are tuples we cannot change these attributes.

>>> = 'Johnny'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

But we can copy the class with the new values.

>>> johnny = john._replace(name='johnny', married=True)
>>> johnny
Person(name='johnny', job_position='developer', sex='male', married=True)

Convert a Dictionary to a Class

Using the same class Person that we defined above.

>>> d = {'name': 'john', 'job_position': 'developer', 'sex': 'male', 'married': False}
>>> john = Person(**d)
>>> john

You can read more about namedtuples here