python - 命名元组中类型名的相关性

标签 python python-3.x typename namedtuple

from collections import namedtuple

Point = namedtuple('whatsmypurpose',['x','y'])
p = Point(11,22)
print(p)

输出:

whatsmypurpose(x=11,y=22)

'whatsmy purpose'的相关性/用途是什么?

最佳答案

namedtuple()tuple 子类的工厂函数。这里,'whatsmy purpose'是类型名称。当您创建命名元组时,会在内部创建一个具有此名称 (whatsmy purpose) 的类。

您可以通过使用如下的详细参数来注意到这一点:

Point=namedtuple('whatsmypurpose',['x','y'], verbose=True)

您也可以尝试 type(p) 来验证这一点。

关于python - 命名元组中类型名的相关性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22762236/

相关文章:

python - Matplotlib donut 标签/注释位置

python - Unicode解码错误: 'charmap' codec can't decode byte 0x98 in position 7736: character maps to <undefined>

python - 无法安装pac-python : File is not a zip file

python - Tensorflow 2.2.0 错误 : [Predictions must be > 0] [Condition x >= y did not hold element-wise:] while using Bidirectional LSTM layer

python-3.x - Pandas Indexing-View-Versus-Copy

python - 按不需要的字母顺序排列的元组

c++ - 为什么使用模板参数的类型成员作为函数的返回类型可以在没有typname关键字的情况下工作,但在其他地方却不起作用?

c++ - 类型名、类型成员和非类型成员 : is it valid code?

python - 具有不同输入的集成模型(预计会看到 2 个数组)