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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:44:34+00:00 2026-06-11T19:44:34+00:00

Using Python 2.6, with the set() builtin, not sets.set. I have defined some custom

  • 0

Using Python 2.6, with the set() builtin, not sets.set.

I have defined some custom data abstraction classes, which will be made members of some sets using the builtin set() object.

The classes are already being stored in a separate structure, before being divided up into sets. All instances of the classes are declared first. No class instances are created or deleted after the first set is declared. No two class instances are ever considered to be “equal” to each other. (Two instances of the class, containing identical data, are considered not the same. A == B is False for all A,B where B is not A.)

Given the above, will there be any reasonable difference between these strategies for testing set_a == set_b?:

Option 1: Store integers in the sets that uniquely identify instances of my class.

Option 2: Store instances of my class, and implement __hash__() and __eq__() to compare id(self) == id(other). (This may not be necessary? Do default implementations of these functions in object just do the same thing but faster?) Possibly use an instance variable that increments every time a new instance calls __init__(). (Not thread safe?)

or,

Option 3: The instances are already stored and looked up in dictionaries keyed by rather long strings. The strings are what most directly represents what the instances are, and are kept unique. I thought storing these strings in the sets would be a RAM overhead and/or create a bunch of extra runtime by calling __eq__() and __hash__(). If this is not the case, I should store the strings directly. (But I think what I’ve read so far tells me it is the case.)

I’m somewhat new to sets in Python. I’ve figured out some of what I need to know already, just want to make sure I’m not overlooking something tricky or drawing a false conclusion somewhere.

  • 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-11T19:44:35+00:00Added an answer on June 11, 2026 at 7:44 pm

    I might be misunderstanding the question, but this is how Python behaves by default:

    class Foo(object):
        pass
    
    a = Foo()
    b = Foo()
    c = Foo()
    
    x = set([a, b])
    y = set([a, b])
    z = set([a, c])
    
    print x == y # True
    print x == z # False
    

    Do default implementations of these functions in object just do the same thing but faster?

    Yes. User-defined classes have __cmp__() and __hash__() methods by default; with them, all objects compare unequal (except with themselves) and x.__hash__() returns id(x). docs

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python logger set up, using python's logging module. I want to
I'm using python, and I have a list of sets, constructed like this: list
I have created a set of Models for my database using Python. Now I'd
Using Python module re, how to get the equivalent of the \w (which matches
Assuming you know about Python builtin property: http://docs.python.org/library/functions.html#property I want to re-set a object
Suppose I am using this data set of movie ratings: http://www.grouplens.org/node/73 It contains ratings
I work on a set of python unit tests that are currently built using
Using Python, what's the correct way to set a file to be read-only when
I'm using Python's csv.reader to bring a set of dates & coordinates from a
I managed to get a set of images loaded using Python. I'd like my

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.