Python namedtuple中typename的相关性

Python namedtuple中typename的相关性,python,python-3.x,typename,namedtuple,Python,Python 3.x,Typename,Namedtuple,输出: from collections import namedtuple Point = namedtuple('whatsmypurpose',['x','y']) p = Point(11,22) print(p) 'whatsmypurpose'的相关性/用途是什么?namedtuple()是tuple子类的工厂函数。这里,“whatsmypurpose”是类型名。创建命名元组时,将在内部创建具有此名称的类(whatsmypurpose) 您可以通过使用以下详细参数来注意到这一点:

输出:

from collections import namedtuple

Point = namedtuple('whatsmypurpose',['x','y'])
p = Point(11,22)
print(p)
'whatsmypurpose'
的相关性/用途是什么?

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

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

whatsmypurpose(x=11,y=22)

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

为新子类提供了其类型名称。从文档中:

collections.namedtuple(typename,字段名,verbose=False,rename=False)
返回名为typename的新元组子类

以下是一个例子:

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

>>> Foo = namedtuple('whatsmypurpose', ['a', 'b'], verbose=True)
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict

class whatsmypurpose(tuple):
    'whatsmypurpose(a, b)'

    __slots__ = ()

    _fields = ('a', 'b')

    def __new__(_cls, a, b):
        'Create new instance of whatsmypurpose(a, b)'
        return _tuple.__new__(_cls, (a, b))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new whatsmypurpose object from a sequence or iterable'
        result = new(cls, iterable)
        if len(result) != 2:
            raise TypeError('Expected 2 arguments, got %d' % len(result))
        return result

    def _replace(_self, **kwds):
        'Return a new whatsmypurpose object replacing specified fields with new values'
        result = _self._make(map(kwds.pop, ('a', 'b'), _self))
        if kwds:
            raise ValueError('Got unexpected field names: %r' % list(kwds))
        return result

    def __repr__(self):
        'Return a nicely formatted representation string'
        return self.__class__.__name__ + '(a=%r, b=%r)' % self

    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values.'
        return OrderedDict(zip(self._fields, self))

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    a = _property(_itemgetter(0), doc='Alias for field number 0')

    b = _property(_itemgetter(1), doc='Alias for field number 1')
这将创建一个类型,它是一个元组子类,它有一个名称
MyClass.\uuuu name\uuuuu==“MyClass”
namedtuple
是一个类型工厂,它还创建元组子类,但是在这个函数API中,您必须显式地传递名称

将返回的类型分配给其他名称时:

class MyClass(tuple):
   pass
这类似于这样做:

Point = namedtuple('whatsmypurpose',['x','y'])
在这两种情况下,您只是将不同的名称别名到类型


通常,您会指定与类型名称相同的名称。如果您担心重复同一个字符串是不正确的,那么您可以使用中的声明性API,而不是
集合中的功能性API。然后你可能会被需要注释类型所困扰。

检查这里:“它变得更具可读性”-因此,它是可读性的。还有更多:“你应该使用命名元组,而不是任何你认为对象符号会使你的代码更具pythonic和可读性的元组。”3.3的可能副本还添加了属性
\u source
,以防你想打印定义或
执行定义。我似乎无法访问该类。也就是说,上面没有定义whatsmypurpose()。该类实际上是在某个地方定义的还是typename仅用于显示目的?@Gadi Point是对上述示例中名为“whatsmypurpose”的类的引用(变量)。因此,一般来说,类名(
Point.\uuuu name\uuuu
)是用于显示的(请尝试
print(Point(1,2))
)。我认为这里需要强调的一点是,Python无法通过查看正在分配类对象的变量的名称来推测新类的名称。变量还不存在!因此,Python需要您显式地告诉它分配新类对象的名称。
Point = namedtuple('whatsmypurpose',['x','y'])
class whatsmypurpose(tuple):
    ... # extra stuff here to setup slots, field names, etc

Point = whatsmypurpose
del whatsmypurpose