Python的namedtuple
工厂功能允许子类它创建将被指定了两次的名称 - 第一对声明的左侧,然后作为该函数的第一参数( IPython的1.0.0,3.3.1的Python):namedtuple - 在相同的定义不同类型的名称的应用程序
In [1]: from collections import namedtuple
In [2]: TypeName = namedtuple('OtherTypeName', ['item'])
我见过的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
我想知道该功能可能有哪些应用程序。