Please consider this code:
>>> point = namedtuple('point', ('x', 'y'))
>>> p1 = point(3,4)
point(x=3, y=4)
>>> id(p1)
2881782633456 # address in memory
>>> p1._replace(x = 78)
point(x=78, y=4)
>>> id(p1)
2881782633456 # same as before.
Seems like i mutated the namedtuple
in-place i.e. it is a mutable object. But it says everywhere that tuple
s and namedtuple
s are both immutable objects. I am confused.
Furthermore, if it is an immutable object, why does it have a _replace
method?