This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Inconsistent Definition of collections.namedtuple.__dict__ in _source
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Jim.Peterson, rhettinger
Priority: normal Keywords:

Created on 2014-04-08 16:35 by Jim.Peterson, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg215772 - (view) Author: Jim Peterson (Jim.Peterson) Date: 2014-04-08 16:35
The result returned by somenamedtuple._source seem inconsistent, in that it defines __dict__ as:

    __dict__ = property(_asdict)

even though it imports property as:

from builtins import property as _property

and the namedtuple fields are defined using _property, instead.  It still compiles and functions properly, but whatever compelled the developer to declare the named fields using _property seems to be ignored in the definition of __dict__.
msg215788 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-04-09 00:55
> 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))
History
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65380
2014-04-09 00:55:22rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg215788
2014-04-08 21:45:34rhettingersetassignee: rhettinger
2014-04-08 21:37:56eric.snowsetnosy: + rhettinger
2014-04-08 16:35:50Jim.Petersoncreate