namedtuple

namedtuple是一个函数,它用来创建一个自定义的tuple对象,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素。

这样一来,我们用namedtuple可以很方便地定义一种数据类型,它具备tuple的不变性,又可以根据属性来引用,使用十分方便。

>>> from collections import namedtuple
>>> Point = namedtuple('Point', ['x', 'y'])
>>> p = Point(1, 2)
>>> p.x
1
>>> p.y
2
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-02
  • 2021-07-27
  • 2021-07-19
  • 2021-11-13
  • 2021-11-26
  • 2022-01-12
猜你喜欢
  • 2022-03-03
  • 2022-03-06
  • 2022-02-08
  • 2021-10-19
  • 2021-07-23
  • 2022-01-07
  • 2021-09-17
相关资源
相似解决方案