collection.namedtuple实例化对象

from collection import namedtuple
# human = namedtuple('human', ['name', 'height', 'age', 'sex'])
h = human('James', 180, 32, 0)
# then you can use h.name, h.sex and so on everywhere.
print(human.name)

from collection import namedtuple
point=namedtuple('point',['x','y'])
p=point(1,2)
print(p.x)
p._replace(x=3)
print(p.x)
p=p._replace(x=3)
print(p.x)

也就是说_replace方法并不是改变point里的x,而是新建了一个p,这个新建的p中的x=3,所以要改变原来的p还必须加上p=p._replace的这样的赋值语句

原文地址:https://www.cnblogs.com/zhangzhaohua/p/10015723.html