[issue16279] namedtuple should compare equality with field names taken into account

Nick Coghlan report at bugs.python.org
Fri Oct 19 11:36:41 CEST 2012


Nick Coghlan added the comment:

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.

----------
nosy: +ncoghlan
resolution:  -> invalid
stage:  -> committed/rejected
status: open -> closed

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


More information about the Python-bugs-list mailing list