This seems like it should be simple:
I want a list like any other list, except it has a different .__str__ method.
- Trying to set
object.__str__ = fooresults in a read-only error - Trying to subclass
listmeans you need some way to convert an existinglistto 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. - Trying to write a wrapper around the
listobject 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!
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.