[New-bugs-announce] [issue32961] namedtuple displaying the internal code

poornaprudhvi report at bugs.python.org
Tue Feb 27 03:55:10 EST 2018


New submission from poornaprudhvi <poornagurram at gmail.com>:

>>> from collections import namedtuple
>>> sample = namedtuple('Name','a','b','c')

This is returning the following code as output:

class Name(tuple):
    'Name(a,)'

    __slots__ = ()

    _fields = ('a',)

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

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

    def __repr__(self):
        'Return a nicely formatted representation string'
        return 'Name(a=%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 Name object replacing specified fields with new values'
        result = _self._make(map(kwds.pop, ('a',), _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

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

----------
assignee: terry.reedy
components: IDLE
messages: 312984
nosy: poornaprudhvi, terry.reedy
priority: normal
severity: normal
status: open
title: namedtuple displaying the internal code
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32961>
_______________________________________


More information about the New-bugs-announce mailing list