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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:04:01+00:00 2026-05-14T05:04:01+00:00

Is there any way to make a list of classes behave like a set

  • 0

Is there any way to make a list of classes behave like a set in python?

Basically, I’m working on a piece of software that does some involved string comparison, and I have a custom class for handling the strings. Therefore, there is an instance of the class for each string.

As a result, I have a large list containing all these classes. I would like to be able to access them like list[key], where in this case, the key is a string the class is based off of (note: the string will never change once the class is instantiated, so it should be hashable).

It seems to me that I should be able to do this somewhat easily, by adding something like __cmp__ to the class, but either I’m being obtuse (likely), or I’m missing something in the docs.

Basically, I want to be able to do something like this (Python prompt example):

>>class a:
... def __init__(self, x):
...  self.var = x
...
>>> from test import a
>>> cl = set([a("Hello"), a("World"), a("Pie")])
>>> print cl
set([<test.a instance at 0x00C866C0>, <test.a instance at 0x00C866E8>, <test.a instance at 0x00C86710>])
>>> cl["World"]
<test.a instance at 0x00C866E8>

Thanks!

Edit Some additional Tweaks:

class a:
... def __init__(self, x):
...     self.var = x
... def __hash__(self):
...     return hash(self.var)
...
>>> v = a("Hello")
>>> x = {}
>>> x[v]=v
>>> x["Hello"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Hello'
>>> x["Hello"]
  • 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-05-14T05:04:01+00:00Added an answer on May 14, 2026 at 5:04 am

    Just write a class that behaves a bit like a mapping:

    class ClassDict(object):
      def __init__(self):
        self.classes = {}
    
      def add(self, cls):
        self.classes[cls.__name__] = cls
    
      def remove(self, cls):
        if self.classes[cls.__name__] == cls:
          del self.classes[cls.__name__]
        else:
          raise KeyError('%r' % cls)
    
      def __getitem__(self, key):
        return self.classes[key]
    
      def __repr__(self):
        return 'ClassDict(%s)' % (', '.join(self.classes),)
    
    class C(object):
      pass
    
    class D(object):
      pass
    
    cd = ClassDict()
    cd.add(C)
    cd.add(D)
    
    print cd
    
    print cd['C']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to make this work in Java? public static void change(List<?
I'm wondering if there's any way to make the text of an unordered list
Is there any way to make two controls that are in different containers the
Is there any easy way to get the list of classes from VS.Net 2008
Is there any way to make a list call a function every time the
Is there any way to make the text align left and justify as same
Is there any way to make a DataTemplate reference itself just from XAML? In
Is there any way to make the form semi-transparent while it is being moved
Is there any way to make the program sleep until an event occurs, but
Is there any way to make the background of the google charts transparent. Google

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.