>>> from collections import namedtuple
>>> Point = namedtuple('Point', ['x', 'y', 'z']) #创建具名元组类
>>> Point
<class '__main__.Point'>
>>> p = Point(3, 4, 5) #实例化对象
>>> p
Point(x=3, y=4, z=5)
>>> p.x #访问成员
3
>>> p._fields #查看字段列表
('x', 'y', 'z')
>>> p._replace(x=30, z=8) #替换成员值,返回新对象
Point(x=30, y=4, z=8)
>>> p
Point(x=3, y=4, z=5)
>>> p.x = 7 #不允许这样直接赋值
AttributeError: can't set attribute>>> hash(p) #可哈希
5050912105994302585>>> x = dict()
>>> x[p] = 3 #可以作为字典的键
>>> x
{Point(x=3, y=4, z=5): 3}
标签:namedtuple,Point,Python,具名,30,元组,._
From: https://blog.51cto.com/u_9653244/6451098