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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:25:26+00:00 2026-06-14T16:25:26+00:00

I have a class. class Part: def __init__(self,name): self.name = name self.count = 0

  • 0

I have a class.

class Part:
    def __init__(self,name):
        self.name = name
        self.count = 0
    def __hash__(self):
        return hash(self.name)
    def __lt__(self,other):
        return self.count < other.count
    def __eq__(self,other):
        return self.name == self.count

I create a bunch of these objects, and populate a list with them. This list ends up containing duplicates. I need that for one segment in my code, but when it comes time to output, I want to only output each part once. So I stick it in a Set.

uniqueParts = set(parts)

I then Iterate over this to write it to file.

for part in uniqueParts:
    f.write(part.name+": "+str(part.count)+'\n')

This does not work. It outputs every duplicate. Since uniqueParts is a set, it must be something wrong with my class where the Set can’t tell that they are duplicates.

Now, I can solve my specific problem several different ways (re-write the class so I don’t need it (data is probably too simple to deserve it’s own class) or iterate through and delete the duplicates, or rework my code so duplicates never occur in the first place) But I will probably run into this problem again in the future, and I’d like to know what I need to do to get Sets to work with classes I’ve written myself.

Thoughts/Assistance?

  • 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-14T16:25:27+00:00Added an answer on June 14, 2026 at 4:25 pm

    This looks wrong:

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

    Maybe you meant:

    def __eq__(self,other):
        return self.name == other.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in django.template: class Template(object): def __init__(self, template_string, origin=None, name='<Unknown
Basically I have the following code: import multiprocessing import time class MyProcess(multiprocessing.Process): def __init__(self,
I have a class like the following: class MyClass(object): def __init__(self, input1, input2): self.attribute1
Hope this is clear enough: class myParent(): def __init__( self ): self.parentNumber = 5
Consider the following python code: class Foo(object): def __init__(self, value): self._value = value @property
I have a MainWindow that looks like this: def __init__(self, parent = None): QMainWindow.__init__(self,
I have the following class (part of it): class SearchViewModel : BaseViewModel<SearchResultItem> { private
I have a simple model class (Part), which pulls from it's information from a
I have a class, which is part of a code library project that was
If I have a .Net class that is not part of any namespace then

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.