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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:46:08+00:00 2026-06-02T22:46:08+00:00

I am writing a custom container class. A constituent object is created independently of

  • 0

I am writing a custom container class. A constituent object is created independently of the container, and can be a member of no container or multiple containers. The container’s public API should support three operations:

  • iteration over all objects
  • insertion of a new object
  • removal of an existing object

The container does some additional work, and its precise implementation may change.

How can I write the public API to this class so that it remains stable as I change the implementation?

If the container is list-like, efficient removal requires the knowledge of the object’s index; knowing the object itself is no good (I don’t want to search the whole container for the element).

If the container is set-like, there’s nothing equivalent to the index, and I need the object itself.

If the container is like a singly linked list, I need some kind of a reference to the object preceding the object being removed.

If the container is like a doubly linked list, I need a reference to the object itself.

I am thinking to have the removal method take a single argument reference, which has no meaning or use outside of the removal method. The iteration would yield a pair of (object, reference).

Is there any problem with this design? Is there an example or design pattern I can look up?

Ideally, I would rather have the iteration yield a complex object that contains both the original object and the reference, and exhibits the interface of both. But I don’t suppose this is doable?

  • 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-02T22:46:09+00:00Added an answer on June 2, 2026 at 10:46 pm

    Here’s what I’ll do unless someone else helps by finding a better solution:

    # to get __hash__ and __eq__ return id(self)
    class Reference:
      def __init__(self, item):
        self.item = item
    
    class RemovalAPI:
      def add_removal_info(self, item, removal_info):
        try:
          references = item.__reference
        except AttributeError:
          references = item.__reference = {}
        references[Reference(self)] = removal_info
    
      def get_removal_info(self, item):
        try:
          references = item.__reference
          self_reference = Reference(self)
          return references[self_reference]
    
    
    
    class Container(list, RemovalAPI):
      def __iter__(self):
        for i in range(len(self)):
          item = self[i]
          self.add_removal_info(item, i)
          yield item
    
      def remove(self, item):
        removal_info = self.get_removal_info(item)
        del self[removal_info]
    
      def insert(self, item):
        self.add_removal_info(item, len(self))
        self.append(item)
        # do whatever post-processing I need
        # ...
    

    If I then decide to change the implementation from list to some other data structure, the public interface can remain unchanged:

    class Container(orderedset, RemovalAPI):
      # inheriting __iter__, remove from parent
      def insert(self, item):
        self.add(item)
        # do whatever post-processing I need
        # ...
    

    Or…

    class Container(linkedlist, RemovalAPI):
      def __iter__(self):
        it = super().__iter__()
        last_item = None
        for item in it:
          self.add_removal_info(item, last_item)
          yield item
    
      def remove(self, item):
        removal_info = self.get_removal_info(item)
        if removal_info is None:
          self.remove_first()
        else:
          self.remove_after(removal_info)
    
      def insert(self, item):
        self.add_removal_info(item, None)
        self.add_to_front(item)
        # do whatever post-processing I need
        # ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a templated C++ generic container class that can optionally maintain its contents
I'm writing the iterator classes for a custom container (some hash map class), let's
I'm writing a custom json deserializer and I'm writing this: public class MyObjectToJson :
I've created a custom check-in policy for TFS by writing a class that extends
I'm writing a custom class library that contains custom controls (not user controls). I
I'm writing an MVC2 app using DataAnnotations. I have a following Model: public class
I'm writing a custom javascript converter and I'm receiving a string that should contain
I am writing a custom NSView subclass. I have several instances of this class
I am writing a custom ConfigurationElementCollection for a custom ConfigurationHandler in C#.NET 3.5 and
I am writing a custom .pac script for use with Firefox. Following numerous examples

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.