Creating a Mutable string class in python -


i trying create class in python have str characteristics , mutable.

example:

>>> = mutablestring("my string") >>> print +"c" "my stringc" >>> a[0] = "a" >>> "ay string" 

how possible inheriting str?

edit: have done 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 case, can do:

a = mutablestring("aaa") a[2] = "b" print #prints aab 

but can't do:

print + "c" #unsupported operand type(s) +: 'mutablestring' , 'str' 

so, i'm trying creating class keep str characteristics, allow me setitem.

i believe give functionality want:

    class mutablestring():         def __init__(self,string):             self.string = list(string)         def concat(self,pos,notherstr): #pos can concatenate want!             self.string[pos] = self.string[pos] + notherstr             return "".join(self.string)          def changestr(self,pos,notherstr):             self.string[pos] = notherstr             return "".join(self.string) 

you can call class, of course have not handled errors pop (such putting in pos larger length of list) leave you.

now say:

    = mutablestring("hello")     a.concat(4,'a')  #output: "helloa"     a.changestr(4,'d') #"helld" 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -