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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:41:42+00:00 2026-06-12T09:41:42+00:00

Is it possible to implement a method of a class so that objects of

  • 0

Is it possible to implement a method of a class so that objects of it works with set() function? So Set(objarray) would return unique objects. Of course, creating my own set is an option, but I just don’t want to reinvent the wheel if it is already there.

EDIT : I think I got your guys confused with my English. This is what I have in my class – I have a Person class that has person name and address as its members. This is what I want to do –

persons = []
for i in range (50):
  name = raw_input("Enter Name")
  address = raw_input("Address")
  persons.append(Person(name,address))
unique = Set(persons) #would only return one person from an address. 
                      #The rest from the same address will be removed

I hope that clear confusion.

  • 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-12T09:41:43+00:00Added an answer on June 12, 2026 at 9:41 am

    Yes. First, if your class implements neither __hash__ nor __eq__, then they are already hashable (the id is used as the hash value and comparisons are done by is).

    Or, if you implement __hash__ and __eq__, then your class instances can be safely used in a set or as dictionary keys:

    >>> class Foo:
    ...     def __init__(self, val): self.val = val
    ...     def __hash__(self): return hash(self.val)
    ...     def __eq__(self, other): return self.val == other.val
    ...     def __repr__(self): return 'Foo(%r)' % self.val
    ... 
    >>> print set([Foo(3), Foo("bar")])
    set([Foo(3), Foo('bar')])
    

    If you want to be able to call set directly on instances of your class, overload __iter__ so that the class instances appear to be iterable:

    >>> class CharSeq:
    ...     def __init__(self, first, last):
    ...         self.first = ord(first)
    ...         self.last = ord(last)
    ...     def __iter__(self):
    ...         return (chr(i) for i in xrange(self.first, self.last+1))
    ... 
    >>> set(CharSeq('a', 'c'))
    set(['a', 'c', 'b'])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to implement Delegate methods for FileSystemWatcher? I have a delegate method
I wanna implement a javascript like method in java , is this possible ?
It is possible to implement a feature that allows users to watch videos as
I'd like to create my own class extending array of ints. Is that possible?
I would like to implement a variation on the Map of Sets collection that
I have a method to return a group of objects as a generic list
Is it possible to define an interface (e.g. MyClass Implements MyInterface) whose method/property definitions
Is it possible to implement some kind of timeout (time limit) for fork using
Is it possible to implement a Facebook Subscribe Button for a Facebook Page rather
Is it possible to implement navigation using jquery ui tabs instead of standard tabs

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.