本文介绍了类型名称在namedtuple中的相关性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from collections import namedtuple

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

输出:

whatsmypurpose(x=11,y=22)

'whatsmypurpose'的意义/用途是什么?

What's the relevance/use of 'whatsmypurpose'?

推荐答案

namedtuple()tuple子类的工厂函数.在这里,'whatsmypurpose'是类型名称.创建命名元组时,将在内部创建具有此名称(whatsmypurpose)的类.

namedtuple() is a factory function for tuple subclasses. Here, 'whatsmypurpose'is the type name. When you create a named tuple, a class with this name (whatsmypurpose) gets created internally.

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

You can notice this by using the verbose argument like:

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

您也可以尝试type(p)进行验证.

Also you can try type(p) to verify this.

这篇关于类型名称在namedtuple中的相关性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 16:13