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

  • Home
  • SEARCH
  • 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 created a class for a dashboard item which will hold information such
I have a struct like this: class Items { private: struct item { unsigned
public class Item { ... } public class Order { public List<Item> Items ...
I have come to see an Installer class item in Visual studio. Why they
Here's a curious one. I have a class A. It has an item of
I have a class which inherits QAbstractTableModel, and holds some complex structs in a
I have a class public class Item : IItem { public virtual Guid Id
I've got a nice little class built that acts as a cache. Each item
Previously, I had a class that wrapped an internal System.Collections.Generic.List<Item> (where Item is a
I have an element that has two classes at all times: <p class=general_description laptop>

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.