Comparing itemgetter objects

I noticed that objects operator.itemgetter

do not define __eq__

, and so their comparison checks id ( is

) by default .

Is there a downside to defining two instances itemgetter

as equal when their initialization argument lists are compared as equal?

Here's one use case for such a comparison. Suppose you have defined 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 the same key functionality (for example, in a statement, assert

or to check if they are safely combined, etc.).

It would be nice if we could answer this question in the affirmative when there are two key functions itemgetter('id')

. But for the time being itemgetter('id') == itemgetter('id')

will be evaluated before False

.

+3


source to share


2 answers


Niklas's answer is pretty smart, but requires a stronger condition as it itemgetter

can take multiple arguments

from collections import defaultdict
from operator import itemgetter
from itertools import count

def cmp_getters(ig1, ig2):
   if any(not isinstance(x, itemgetter) for x in (ig1, ig2)):
      return False
   d1 = defaultdict(count().next)
   d2 = defaultdict(count().next)
   ig1(d1)                                 # populate d1 as a sideeffect
   ig2(d2)                                 # populate d2 as a sideeffect
   return d1==d2

      



Some test files

>>> cmp_getters(itemgetter('foo'), itemgetter('bar'))
False
>>> cmp_getters(itemgetter('foo'), itemgetter('bar','foo'))
False
>>> cmp_getters(itemgetter('foo','bar'), itemgetter('bar','foo'))
False
>>> cmp_getters(itemgetter('bar','foo'), itemgetter('bar','foo'))
True

      

+4


source


itemgetter

returns the callee. I hope you don't want to compare the files being called . Right? Since the returned callable identifier is not supported, it will be the same even if you pass the same arguments.

def fun(a):
    def bar(b):
        return a*b
    return bar

a = fun(10)
print id(a(10))

a = fun(10)
print id(a(10))

      



On the other hand, when you use an itemgetter called as an accessor to access the underlying object, then this object comparison will be used to perform the comparison. This is shown in the Sorting Howto using the work unit functions .

0


source







All Articles