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: namedtuple should compare equality with field names taken into account
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Julian, ncoghlan, rhettinger
Priority: normal Keywords:

Created on 2012-10-18 19:59 by Julian, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg173292 - (view) Author: Julian Berman (Julian) * Date: 2012-10-18 19:59
I find the following to be unintuitive:

Python 3.3.0rc1 (default, Sep  6 2012, 16:02:32) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import namedtuple
>>> F = namedtuple("F", "x")
>>> G = namedtuple("G", "y")
>>> F(12) == G(12)
True

I'm OK with not taking the class name into account, that sounds reasonable, but I think field names should make those unequal.
msg173316 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-10-19 09:36
So that they can be returned from an API without breaking backwards compatibility, named tuples are intentionally equivalent to the corresponding ordinary tuple:

>>> from collections import namedtuple
>>> Pair = namedtuple("Pair", "x y")
>>> Pair(1, 2) == (1, 2)
True

Transitivity thus requires that two named tuples with different field names also compare equal with each other:

>>> Pair2 = namedtuple("Pair2", "a b")
>>> Pair(1, 2) == (1, 2) == Pair2(1, 2)
True
>>> Pair(1, 2) == Pair2(1, 2)
True

So the field names on named tuples are just an access mechanism - they aren't part of the value of the instances.
msg173317 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-10-19 09:37
"... returned from an API that previously returned an ordinary tuple ..."
msg173575 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-10-23 01:42
Nick, thanks for closing this one with a clear and accurate explanation.
History
Date User Action Args
2022-04-11 14:57:37adminsetgithub: 60483
2012-10-23 01:42:26rhettingersetmessages: + msg173575
2012-10-19 09:37:20ncoghlansetmessages: + msg173317
2012-10-19 09:36:41ncoghlansetstatus: open -> closed

nosy: + ncoghlan
messages: + msg173316

resolution: not a bug
stage: resolved
2012-10-18 19:59:43Juliancreate