I noticed that operator.itemgetter objects don’t define __eq__, and so their comparison defaults to checking identity (is).
Is there any disadvantage to defining two itemgetter instances as equal whenever their initialization argument lists compare as equal?
Here’s one use case of such a comparison. Suppose you define a sorted data structure whose constructor requires a key function to define the sort. Suppose you want to check if two such data structures have identical key functions (e.g., in an assert statement; or to verify that they can be safely merged; etc.).
It would be nice if we could answer that question in the affirmative when the two key functions are itemgetter('id'). But currently, itemgetter('id') == itemgetter('id') would evaluate to False.
Niklas’s answer is quite clever, but needs a stronger condition as
itemgettercan take multiple argumentsSome testcases