[issue21181] Inconsistent Definition of collections.namedtuple.__dict__ in _source

Raymond Hettinger report at bugs.python.org
Wed Apr 9 02:55:22 CEST 2014


Raymond Hettinger added the comment:

> whatever compelled the developer to declare the named fields 
> using _property seems to be ignored in the definition of __dict__.

What compelled the _property alias is that the user could name an attribute "property" which would cause a conflict if _property has not been renamed.  

For example:

   T = namedtuple('T', ['property', 'plant', 'equipment'])

would create the following field definitions:

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

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

    equipment = _property(_itemgetter(2), doc='Alias for field number 2')

Note, if we didn't use _property, the builtin property() would be shadowed.

The code for __dict__ occurs upstream (before the field definitions), so it is safe from redefinition:

    @property
    def __dict__(self):
        'A new OrderedDict mapping field names to their values'
        return OrderedDict(zip(self._fields, self))

----------
resolution:  -> invalid
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21181>
_______________________________________


More information about the Python-bugs-list mailing list