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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:50:17+00:00 2026-05-11T18:50:17+00:00

This seems like it should be simple: I want a list like any other

  • 0

This seems like it should be simple:

I want a list like any other list, except it has a different .__str__ method.

  1. Trying to set object.__str__ = foo results in a read-only error
  2. Trying to subclass list means you need some way to convert an existing list to an instance of the subclass. This requires either copying all attributes manually (a huge pain), or somehow copying them all automatically, which I don’t know how to do.
  3. Trying to write a wrapper around the list object means I have to figure out some way to send all messages to the wrapped object except .__str__ which I handle with my own method. Don’t know how to do this.

Any alternatives, or solutions #2 or #3 greatly appreciated. Thanks!

  • 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-11T18:50:17+00:00Added an answer on May 11, 2026 at 6:50 pm

    This solution works without a wrapper. And works if you join two lists by add. Any operation that modify the list itself will work as expected. Only functions that return a copy of the list like: sorted, reveresed will return the native python list which is fine. sort and reverse on the other hand operate on the list itself and will keep the type.

    class myList(list):
        def __new__(cls, data=None):
            obj = super(myList, cls).__new__(cls, data)
            return obj
    
        def __str__(self):
            return 'myList(%s)' % list(self)
    
        def __add__(self, other):
            return myList(list(self) + list(other))
    
    >>> l = myList(range(5))
    >>> print l
    myList([0, 1, 2, 3, 4])
    >>> print l + [1, 2]
    myList([0, 1, 2, 3, 4, 1, 2])
    >>> l.sort()
    >>> print l
    myList([0, 1, 2, 3, 4])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should be really simple, but I'm unable to figure this
This seems like it should be so simple, but apparently it isn't. I have
This seems like it should be very simple but I can't get it to
Seems like this should be simple, but powershell is winning another battle with me.
This feels like it should be pretty simple, but not much seems to be
It seems like it should be a simple concept. I need a vertical list.
This seems like it should be obvious but I can't figure it out. Suppose
This seems like it should be something I already know. We need to run
This seems like it should be something very easy to do, but every time
This seems like it should be an easy thing to do, but for the

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.