擅长:python、mysql、java
<p>您还可以使用<code>collection</code>标准模块中的“命名元组”。命名元组的工作方式类似于“普通”元组,但元素有名称,您可以使用“点语法”访问元素。从<a href="https://docs.python.org/3/library/collections.html?highlight=named%20tuple#collections.namedtuple" rel="nofollow">collection docs</a>:</p>
<pre><code>>>> # Basic example
>>> Point = namedtuple('Point', ['x', 'y'])
>>> p = Point(11, y=22) # instantiate with positional or keyword arguments
>>> p[0] + p[1] # indexable like the plain tuple (11, 22)
33
>>> x, y = p # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y # fields also accessible by name
33
>>> p # readable __repr__ with a name=value style
Point(x=11, y=22)
</code></pre>