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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:49:35+00:00 2026-06-14T13:49:35+00:00

I need a python list object which, upon insert, automatically checks for certain constraints

  • 0

I need a python list object which, upon insert, automatically checks for certain constraints of the form: “A must always come before B” or “If C is included, it must always come last”.

What’s the easiest/fastest way to go about implementing this. The obvious approach is to override all the methods of the list data type which alter its contents (append, extend, insert, etc), and verify that constraints still hold after the operation. It’s just that this is pretty tedious as there are a lot of these methods. Is there an easier way?

  • 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-14T13:49:36+00:00Added an answer on June 14, 2026 at 1:49 pm

    I would strongly recommend subclassing from the collections.MutableSequence abstract base class. The drawback is that it won’t be recognized as a subclass of list (as user4815162342 points out). However, that should almost never matter as long as people using the resulting class are doing the right thing (i.e. using duck typing or passing abstract base classes rather than concrete classes to isinstance).

    The wonderful thing about this is that once you’ve defined the following methods, you get the rest of the MutableSequence interface for free. Here’s a concrete subclass of MutableSequence that you can use as a template for further customization. In your case, you should only have to customize __init__, __setitem__, insert, and __delitem__. Everything else is defined in terms of those, and so will perform whatever checks you insert:

    import collections
    class MyList(collections.MutableSequence):
        def __init__(self, it=()):
            self._inner = list(it)
        def __len__(self):
            return len(self._inner)
        def __iter__(self):
            return iter(self._inner)
        def __contains__(self, item):
            return item in self._inner
        def __getitem__(self, index):
            return self._inner[index]
        def __setitem__(self, index, value):
            self._inner[index] = value
        def __delitem__(self, index):
            del self._inner[index]
        def __repr__(self):
            return 'MyList({})'.format(self._inner)
        def insert(self, index, item):
            return self._inner.insert(index, item)
    

    A couple of simple tests:

    >>> ml = MyList('foo')
    >>> ml
    MyList(['f', 'o', 'o'])
    >>> ml.append(5)
    >>> ml
    MyList(['f', 'o', 'o', 5])
    >>> ml.reverse()
    >>> ml
    MyList([5, 'o', 'o', 'f'])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python list with a number of entries, which I need to
I have passed Python list to template like l=['text','there is no salary','I need alcohol','and
I've been playing with Python and I have this list that I need worked
my_list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven'] Using python, I need list
Using Python I need to insert a newline character into a string every 64
I am writing a Python package which reads the list of modules (along with
I'm using python for my shopping cart class which has a list of items.
I have a class that subclasses the list object. Now I need to handle
We have some code which creates a python list and then appends data items
I am in need of a Python (2.7) object that functions like a set

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.