Python中的“命名元组”是什么? - What are “named tuples” in Python?
问题:
Reading the changes in Python 3.1 , I found something... unexpected: 阅读Python 3.1中的更改后 ,我发现了一些意外……
The sys.version_info tuple is now a named tuple : sys.version_info元组现在是一个命名的元组 :
I never heard about named tuples before, and I thought elements could either be indexed by numbers (like in tuples and lists) or by keys (like in dicts). 我以前从未听说过命名元组,并且我认为元素可以通过数字(例如在元组和列表中)或通过键(例如在字典中)进行索引。 I never expected they could be indexed both ways. 我从未想到它们可以同时被索引。
Thus, my questions are: 因此,我的问题是:
- What are named tuples? 什么叫元组?
- How to use them? 如何使用它们?
- Why/when should I use named tuples instead of normal tuples? 为什么/何时应该使用命名元组而不是普通元组?
- Why/when should I use normal tuples instead of named tuples? 为什么/何时应该使用普通元组而不是命名元组?
- Is there any kind of "named list" (a mutable version of the named tuple)? 是否有某种“命名列表”(命名元组的可变版本)?
解决方案:
参考一: https://stackoom.com/question/CSn2/Python中的-命名元组-是什么
参考二: https://oldbug.net/q/CSn2/What-are-named-tuples-in-Python