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 9222663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:53:09+00:00 2026-06-18T03:53:09+00:00

I have and instance of collections.Counter class, also i have some objects like: p1

  • 0

I have and instance of collections.Counter class, also i have some objects like:

p1 = Person(name='John')
p2 = Person(name='John')
p3 = Person(name='Jane')

I want to hold the counts for this person objects in a instance of Counter, taking into account that person objects with same names must increment the same person count, so if i have a list with all person objects:

people = [p1, p2, p3]

and i populate my counter with it:

c = Counter(people)

i want to get the following:

c[p1] #prints 2
c[p2] #prints 2
c[p3] #prints 1

My first attempt was to implement a new __eq__ method for person objects

def __eq__(self, other):
  return self.name == other.name

I thought that this could work because counter objects seems like increment the counts
for keys based on the equality of the key objects, like in:

c = Counter(['A', 'A', 'B'])
c['A'] #prints 2
c['B'] #prints 1

Another attempt could be inherit from Counter and override the underlying method that
Counter uses for measure the equality between objects , i am not sure but i think
Counter uses __contains__ method for that.

My question is if there are any way to gets this behavior without using inheritance and if not, what could be the best way to do it?.

  • 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-18T03:53:10+00:00Added an answer on June 18, 2026 at 3:53 am

    You also have to implement __hash__:

    class Person(object):
        def __init__(self, name=None, address=None):
            self.name = name
            self.address = address
    
        def __eq__(self, other):
            return self.name == other.name and self.address == other.address
    
        def __hash__(self):
            return hash((self.name, self.address))
    

    Now your code works:

    >>> Counter(people)
    Counter({<__main__.Person object at 0x24a7590>: 2, <__main__.Person object at 0x24a75d0>: 1})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with some collections in them, and I would like to
I have an instance of the Popen class created through subprocess.Popen. I would like
I have an instance of Class A that I want to refer to in
I have a class that exposes a fluent interface style that I also want
I have a class A which holds a collection of objects. Every instance of
I have System.Collections.Generic.Dictionary<A, B> dict where A and B are classes, and an instance
I have an instance method that I'd like to invoke directly using the callback
I have an instance of UIToolbar that contains a UITextField inside. I'd like to
Let's assume I have a class SuperPower, and that I want to put my
I have following instance of System.Collections.Specialized.NameValueCollection: Dim UserSelection As New System.Collections.Specialized.NameValueCollection UserSelection.Add(D_Color1, Black) UserSelection.Add(D_Color2,

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.