Thanks to an amazing tweet of Sam & Max, I have read an article about how to start with Python in 2019 which give me the opportunity to watch the video Beyond PEP8 from Raymond Hettinger.
This is how I discover and understand how to use the NamedTuple
Before
1
2
3
4
5
p = (120, 50, 100)
if p[0] > 100:
print('do something')
if p[1] <= 50:
print('do something else')
This is how it works with namedtuple
1
2
3
4
5
6
7
8
9
from collections import namedtuple
color = namedtuple('color', ['hue', 'saturation', 'luminosity'])
p = color(170, 20, 50)
if p.hue > 100:
print('do something')
if p.saturation <= 50:
print('do something else')
you should use named tuples instead of tuples anywhere you think object notation will make your code more pythonic and more easily readable.
If you need more details or/and advance tips: PSF documentation