Enum vs Named tuple
- enums are a way of aliasing values,
- namedtuple is a way of encapsulating data by name.
- The two are not really interchangeable,
from collections import namedtuple
from enum import Enum
class HairColor(Enum):
blonde = 1
brown = 2
black = 3
red = 4
Person = namedtuple('Person', ['name','age','hair_color'])
bert = Person('Bert', 5, HairColor.black)
you can use enums as values in namedtuple.
No comments:
Post a Comment