Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python collections.namedtuple()混淆_Python_Namedtuple - Fatal编程技术网

python collections.namedtuple()混淆

python collections.namedtuple()混淆,python,namedtuple,Python,Namedtuple,声明任何python有效标识符都可以是字段名,但以下划线开头的除外,这很好 如果rename参数为true,它将用有效的字段名替换无效的字段名,但是在这里指定的示例中,它将用\u 1或\u 3替换它,这是怎么回事?这些以下划线开头 文件还说: 如果verbose为true,则类定义将在生成之前打印 这到底意味着什么?不能在名称开头使用下划线的原因是这些下划线可能与类提供的方法名称冲突(例如\u replace) 由于数字不是有效的Python名称,因此任何作为属性无效的名称(因此不是有效的Pyt

声明任何python有效标识符都可以是字段名,但以下划线开头的除外,这很好

如果
rename
参数为true,它将用有效的字段名替换无效的字段名,但是在这里指定的示例中,它将用
\u 1
\u 3
替换它,这是怎么回事?这些以下划线开头

文件还说:

如果verbose为true,则类定义将在生成之前打印


这到底意味着什么?

不能在名称开头使用下划线的原因是这些下划线可能与类提供的方法名称冲突(例如
\u replace

由于数字不是有效的Python名称,因此任何作为属性无效的名称(因此不是有效的Python标识符或以下划线开头的名称)都将替换为下划线+位置号。这意味着这些生成的名称不能与有效名称冲突,也不能与类型上提供的方法冲突

这与您可以选择的名称并不矛盾;事实上,考虑到这些限制,这是一个完美的退路。此外,由此产生的名称很容易推断;这些值的属性与它们在元组中的索引直接相关

至于将
verbose
设置为
True
,则按照tin上的说明执行。生成的
namedtuple
类的源代码打印到
sys.stdout

>>> from collections import namedtuple
>>> namedtuple('foo', 'bar baz', verbose=True)
class foo(tuple):
    'foo(bar, baz)'

    __slots__ = ()

    _fields = ('bar', 'baz')

    def __new__(_cls, bar, baz):
        'Create new instance of foo(bar, baz)'
        return _tuple.__new__(_cls, (bar, baz))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new foo 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 __repr__(self):
        'Return a nicely formatted representation string'
        return 'foo(bar=%r, baz=%r)' % self

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

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

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

    __dict__ = _property(_asdict)

    def __getstate__(self):
        'Exclude the OrderedDict from pickling'
        pass

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

    baz = _property(_itemgetter(1), doc='Alias for field number 1')


<class '__main__.foo'>
>>从集合导入名为tuple的
>>>namedtuple('foo','bar baz',verbose=True)
类foo(元组):
‘foo(bar,baz)’
__插槽\uuux=()
_字段=('bar','baz')
def _;新__;(cls、baz、baz):
'创建foo(bar,baz)的新实例'
返回元组。新的(_cls,(bar,baz))
@类方法
定义生成(cls,iterable,new=tuple.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,len=len):
'从序列或iterable创建新的foo对象'
结果=新的(cls,iterable)
如果len(结果)!=2:
raise TypeError('应为2个参数,获得%d'%len(结果))
返回结果
定义报告(自我):
'返回格式良好的表示字符串'
返回'foo(bar=%r,baz=%r)'%self
def_asdict(self):
'返回将字段名称映射到其值的新OrderedDict'
退货订单DICT(邮政编码(self.\u字段,self))
def _更换(_self,**kwds):
'返回用新值替换指定字段的新foo对象'
结果=_self._make(map(kwds.pop,('bar','baz'),_self))
如果kwds:
raise VALUERROR('获得意外字段名:%r'%kwds.keys())
返回结果
def uu getnewargs uu(self):
'将self作为普通元组返回。用于复制和pickle。”
返回元组(self)
__dict\uuu=\u属性(\u asdict)
定义获取状态(自身):
'从酸洗中排除订购的DICT'
通过
bar=\u属性(\u itemgetter(0),doc='Alias for field number 0')
baz=\u属性(\u itemgetter(1),doc='1号字段的别名〕
这允许您检查为类生成的内容