I am trying to create a class in python that will have all the str
characteristics , but will also be mutable.
Example:
>>> a = mutableString("My string")
>>> print a +"c"
"My stringc"
>>> a[0] = "a"
>>> a
"ay string"
How is it possible by inheriting from str
?
Edit: What i Have done so far is:
class mutableString(object):
def __init__(self, string):
self.string = string
def __setitem__(self, item, value):
self.string = self.string[:item] + value + self.string[item + len(value):]
print type(self.string)
def __repr__(self):
return self.string
In this case, i can do:
a = mutableString("aaa")
a[2] = "b"
print a
#prints aab
but I can't do:
print a + "c"
#unsupported operand type(s) for +: 'mutableString' and 'str'
So, what I'm trying to do is creating a class that would keep str
characteristics, but allow me to setitem.