Q:

If a namedtuple is immutable, why does it have a _replace method?

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 tuples and namedtuples are both immutable objects. I am confused. 
Furthermore, if it is an immutable object, why does it have a _replace method? 

A:

Tuple is definitely immutable. If you print the p1, its same even after _replace function. 
>>> point = namedtuple('point', ('x', 'y'))
>>> p1 = point(3,4)
>>> p1
point(x=3, y=4)   #same before _replace
>>> p1._replace(x = 78)
point(x=78, y=4)
>>> p1
point(x=3, y=4)  #same after _replace

why do you have _replace then?
It aims to return a new instance of the named tuple replacing specified fields with new values. It does not mutate the actual tuple itself. The operation is short lived.

