Python collections.namedtuple如何工作?

Python collections.namedtuple如何工作?,python,namedtuple,Python,Namedtuple,我正在学习Python中的“namedtuple”代码(Python3.6.3)。我运行代码: from collections import namedtuple,_iskeyword Point = namedtuple('Point', ['x', 'y'],rename=False,verbose=True) p = Point(2,3) print(p) 然后控制台打印,例如: from builtins import property as _property, tuple as

我正在学习Python中的“namedtuple”代码(Python3.6.3)。我运行代码:

from collections import namedtuple,_iskeyword
Point = namedtuple('Point', ['x', 'y'],rename=False,verbose=True)
p = Point(2,3)
print(p)
然后控制台打印,例如:

from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict

class Point(tuple):
    'Point(x, y)'

    __slots__ = ()

    _fields = ('x', 'y')

    def __new__(_cls, x, y):
        'Create new instance of Point(x, y)'
        return _tuple.__new__(_cls, (x, y))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new Point 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 Point object replacing specified fields with new values'
        result = _self._make(map(kwds.pop, ('x', 'y'), _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__ + '(x=%r, y=%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)

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

    y = _property(_itemgetter(1), doc='Alias for field number 1')
这是类的定义,我对类的定义感到困惑:

x = _property(_itemgetter(0), doc='Alias for field number 0')
在这里,属性为
\u property
,属性的
fget
函数为
\u itemgetter(0)

我的问题是:

什么是
\u itemgetter(0)
的返回?

在这种情况下,
\u属性如何工作?

简短回答 它允许您使用
p[0]
以及
p.x
检索值
2

>>> p.x
2
>>> p[0]
2
解释
属性允许您在不使用
()
的情况下调用方法。 因此
p.x
而不是
p.x()

itemgetter(0)
是用于
[]
索引语法的函数。 在本例中,它从底层元组获取该索引处的元素

它返回一个新函数:

>>> f = _itemgetter(0)
>>> f(t)
10
调用此函数:

>>> f = _itemgetter(0)
>>> f(t)
10
具有与以下相同的效果:

>>> t[0]
10
最后,
属性
使其“可调用”,而无需在末尾添加
()

简短回答 它允许您使用
p[0]
以及
p.x
检索值
2

>>> p.x
2
>>> p[0]
2
解释
属性允许您在不使用
()
的情况下调用方法。 因此
p.x
而不是
p.x()

itemgetter(0)
是用于
[]
索引语法的函数。 在本例中,它从底层元组获取该索引处的元素

它返回一个新函数:

>>> f = _itemgetter(0)
>>> f(t)
10
调用此函数:

>>> f = _itemgetter(0)
>>> f(t)
10
具有与以下相同的效果:

>>> t[0]
10

最后,
属性
使其“可调用”,而无需在末尾添加
()

谢谢。更正。在code x=_属性(_itemgetter(0),…)中,它不会将实例传递给_itemgetter,您的意思是它传递underyingwith
p.x
类型的实例,您调用
p.。_itemgetter(0)(
),即绑定方法。这相当于
\u itemgetter(0)(p)
。谢谢。更正。在code x=_属性(_itemgetter(0),…)中,它不会将实例传递给_itemgetter,您的意思是它传递underyingwith
p.x
类型的实例,您调用
p.。_itemgetter(0)(
),即绑定方法。这相当于
\u itemgetter(0)(p)