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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:13:51+00:00 2026-05-21T16:13:51+00:00

My class Item describes certain items, which are naturally known by their numeric ID

  • 0

My class Item describes certain items, which are naturally known by their numeric ID (0, 1, 2, …). I have implemented a registry inside the class, which indexes all the objects by their ID. I also have a method that retrieves an object by its ID:

class Item
    _registry = []
    def __init__(self):
        self._registry.append(self)
        # ...
    @classmethod
    def get_item_by_id(cls, k):
        return cls._registry[k]
    # ...

Is this a reasonable design?

Assuming it is, would it be ok to allow subscripts for the Item class itself? I can (I think) define __getitem__ in Item‘s metaclass, thus allowing the syntax Item[2] instead of Item.get_item_by_id(2).

If it has any problems (e.g., too weird, can cause undesirable side effects, etc), please let me know.

  • 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-21T16:13:52+00:00Added an answer on May 21, 2026 at 4:13 pm

    Nope. That’s perfectly fine. It’s definitely better than the long form. Just make sure you return sensible exceptions (a KeyError with a descriptive message, e.g. Invalid id for element) when someone uses an incorrect index. Also, make sure that you document this behavior, otherwise there’d be no point in making the object subscriptable in the first place.

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

Sidebar

Related Questions

I have the jquery: $(.item) which gives me all elements of class item. I
I have a datastore model representing items in an ecommerce site: class Item(db.Model): CSIN
public class Item { ... } public class Order { public List<Item> Items ...
I have come to see an Installer class item in Visual studio. Why they
I have some custom type: [RdfSerializable] public class Item { [RdfProperty(true)] public string Name
My model: class User has_many :items end class Item belongs_to :user def self.with_color(color, is_loaded)
I have a Model with a Foreign Key of Parent class Item(models.Model): parent =
I have that situation: I have models Item, Region and Country. class Item(models.Model): name
I have some python module, which has a class ModuleClass and I can't modify
I have a class which inherits QAbstractTableModel, and holds some complex structs in a

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.