---
title: "Discussion on \"Everything You Need to Know About Python's Namedtuples\" | Hashnode"
description: "Discussion on \"Everything You Need to Know About Python's Namedtuples\". Learn the most important stuff about named tuples in Python. Learn to convert a namedtuple to dict and json. Add optional fields, docstring, and more!"
url: "https://hashnode.com/posts/everything-you-need-to-know-about-pythons-namedtuples/5f6e3f4fade1fb598d439946"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Everything You Need to Know About Python's Namedtuples](https://miguendes.me/everything-you-need-to-know-about-pythons-namedtuples?utm_source=hashnode&utm_medium=feed)

Learn the most important stuff about named tuples in Python. Learn to convert a namedtuple to dict and json. Add optional fields, docstring, and more!

Sep 25, 2020 · 15 min read · miguendes.me  
Updated Jul 23, 2022  
Read the article

-   [#python](https://hashnode.com/tag/python)
-   [#beginners](https://hashnode.com/tag/beginners)
-   [#clean-code](https://hashnode.com/tag/clean-code)

## Written by

[Miguel Brito](https://hashnode.com/@miguendes)

@miguendes · London, UK

  
[View profile](https://hashnode.com/@miguendes)

Machine Learning Software Engineer - Hobbyst Technical Writer - Interested in Software Testing, Best Practices, Scalability, and Python.

AI Software Engineer based in London, UK with 6+ years of professional experience developing and releasing software in different programming languages - Hobbyist Technical Writer - Interested in Software Testing, Best Practices, Scalability, Machine Learning/AI, and Python.

## Responses 4

Markdown supported · ⌘↵ to post

[Roach Zhao](https://hashnode.com/@roachzhao)

[Nov 29, 2020](https://hashnode.com/posts/everything-you-need-to-know-about-pythons-namedtuples/5f6e3f4fade1fb598d439946/comment/5fc39b5a4b7d527151c38274)

Hi, could you tell me why I got error for optional attribute?

```
>>> from dataclasses import dataclass
>>> from typing import Optional
>>> @dataclass
... class Color:
...     r: float
...     g: float
...     b: float
...     alpha: Optional[float]
... 
>>> blue = Color(r=0, g=0, b=255)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() missing 1 required positional argument: 'alpha'
```

Thansk in advance!

[Miguel Brito](https://hashnode.com/@miguendes)

[Nov 29, 2020](https://hashnode.com/posts/everything-you-need-to-know-about-pythons-namedtuples/5f6e3f4fade1fb598d439946/comment/5fc39b5a4b7d527151c38274#reply-5fc3ef91bc24037614df811a)

Hi [Roach Zhao](https://hashnode.com/@roachzhao), alpha must be `None` for it to work.

If you make it like this `alpha: Optional[float] = None` then your code will work. I've updated the article, thanks for you comment.

```
In [1]: from dataclasses import dataclass
   ...: from typing import Optional

In [2]: 

In [2]: @dataclass
   ...: class Color:
   ...:     """A regular class that represents a color."""
   ...:     r: float
   ...:     g: float
   ...:     b: float
   ...:     alpha: Optional[float] = None
   ...: 

In [3]: blue = Color(r=0, g=0, b=255)

In [4]: blue
Out[4]: Color(r=0, g=0, b=255, alpha=None)
```

[Roach Zhao](https://hashnode.com/@roachzhao)

[Nov 30, 2020](https://hashnode.com/posts/everything-you-need-to-know-about-pythons-namedtuples/5f6e3f4fade1fb598d439946/comment/5fc39b5a4b7d527151c38274#reply-5fc4a50a0a072175ef2cebe5)

Thanks [Miguel Brito](https://hashnode.com/@miguendes) !

[Paddy McCarthy](https://hashnode.com/@Paddy3118)

[Oct 11, 2020](https://hashnode.com/posts/everything-you-need-to-know-about-pythons-namedtuples/5f6e3f4fade1fb598d439946/comment/5f82da7d5841661b519d0f8c)

You stated:

> To add a **hash** method, you need to make them immutable by setting unsafe\_hash to True

The logic doesn't seem to fit the naming, and from how I interpret the [docs](https://docs.python.org/3/library/dataclasses.html?highlight=unsafe_hash) you might do it this way but it is frowned on:

> Although not recommended, you can force dataclass() to create a **hash**() method with unsafe\_hash=True. This might be the case if your class is logically immutable but can nonetheless be mutated. This is a specialized use case and should be considered carefully.

It might be better to look at the settings of the eq and frozen parameters when `unsafe_hash=False`, (the default): Setting `eq=True, frozen=True` would then get a **hash** method generated.

Gosh, it takes my concentration to work this out :-)

[Miguel Brito](https://hashnode.com/@miguendes)

[Oct 17, 2020](https://hashnode.com/posts/everything-you-need-to-know-about-pythons-namedtuples/5f6e3f4fade1fb598d439946/comment/5f82da7d5841661b519d0f8c#reply-5f8abbca6eb39417ce5e5d15)

Thanks for the heads up [Paddy McCarthy](https://hashnode.com/@Paddy3118). It's a bit confusing indeed. I'll run some experiments and update the post.

[Terry](https://hashnode.com/@terrd)

[Sep 29, 2020](https://hashnode.com/posts/everything-you-need-to-know-about-pythons-namedtuples/5f6e3f4fade1fb598d439946/comment/5f73a1a7f955ec0a130f6ac3)

Note that field ordering of `dict`s is only maintained in newer python versions ([officially since 3.7, but also in CPython 3.6](https://stackoverflow.com/a/39980744/9560908)).

```
>>> c = {"r": 50, "g": 205, "b": 50, "alpha": alpha}
>>> Color = namedtuple("Color", c)
>>> Color(**c)
Color(r=50, g=205, b=50, alpha=0)
```

[Miguel Brito](https://hashnode.com/@miguendes)

[Oct 1, 2020](https://hashnode.com/posts/everything-you-need-to-know-about-pythons-namedtuples/5f6e3f4fade1fb598d439946/comment/5f73a1a7f955ec0a130f6ac3#reply-5f7622deb7a1d82bf9b34fc1)

Hi [Terry](https://hashnode.com/@terrd), yes, you are right about the order. This example was aimed to demonstrate how to create the named tuple regardless of the key ordering. The idea is that we always use it by providing keyword arguments. This way the order doesn't make a difference. Like this:

```
In [2]: alpha = 0.3

In [3]: c = {"r": 50, "g": 205, "b": 50, "alpha": alpha}

In [4]: from collections import namedtuple

In [5]: Color = namedtuple("Color", c)

In [6]: Color(**c)
Out[6]: Color(r=50, g=205, b=50, alpha=0.3)

In [7]: Color(g=205, r=50, b=50, alpha=0.2)
Out[7]: Color(r=50, g=205, b=50, alpha=0.2)
```

[Paula Maranhão](https://hashnode.com/@pmaranhaor)

[Sep 25, 2020](https://hashnode.com/posts/everything-you-need-to-know-about-pythons-namedtuples/5f6e3f4fade1fb598d439946/comment/5f6e578b60599c596c47281f)

Great! Keep up with the good work!
