相关疑难解决方法(0)

namedtuple — 同一定义中不同类型名称的应用程序

Pythonnamedtuple工厂函数允许它创建的子类的名称被指定两次——首先在声明的左侧,然后作为函数的第一个参数(IPython 1.0.0,Python 3.3.1):

In [1]: from collections import namedtuple

In [2]: TypeName = namedtuple('OtherTypeName', ['item'])
Run Code Online (Sandbox Code Playgroud)

我在 docs.python.org 站点上看到的所有示例在两个位置都使用相同的名称。但是可以使用不同的名称,它们的功能也不同:

In [3]: TypeName(1)
Out[3]: OtherTypeName(item=1)

In [4]: type(TypeName)
Out[4]: builtins.type

In [5]: type(OtherTypeName)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-6616c1214742> in <module>()
----> 1 type(OtherTypeName)

NameError: name 'OtherTypeName' is not defined

In []: OtherTypeName(1)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-47d6b5709a5c> in <module>()
----> 1 OtherTypeName(1)

NameError: name 'OtherTypeName' is not defined
Run Code Online (Sandbox Code Playgroud)

我想知道此功能可能有哪些应用程序。

python namedtuple

5
推荐指数
1
解决办法
2615
查看次数

标签 统计

namedtuple ×1

python ×1