Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7721785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:00:49+00:00 2026-06-01T04:00:49+00:00

I noticed that operator.itemgetter objects don’t define __eq__ , and so their comparison defaults

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:00:50+00:00Added an answer on June 1, 2026 at 4:00 am

    Niklas’s answer is quite clever, but needs a stronger condition as 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 testcases

    >>> 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently used the shift operators in Java and noticed that the >> operator
I have noticed that when I type an operator in REPL, it is often
I just noticed that we can access c++ static member function by member-selection operator
I notice that the character/symbol '`' and '@' is not used as an operator
I just noticed that you can not use standard math operators on an enum
I noticed that this recipe seems to use __rlshift__ , __ror__ like operators. But,
Possible Duplicate: The written versions of the logical operators. I notice that C++ define
I read here about std::auto_ptr<>::operator= Notice however that the left-hand side object is not
I noticed that Google maps is providing directions in my local language (hungarian) when
I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.