Re: str and __setitem__

2007-01-25 Thread bearophileHUGS
Peter Otten: > >>> class mutable_str(object):... def __init__(self, value): > ... self._value = value > ... def __setitem__(self, index, value): > ... self._value = self._value[:index] + value + > self._value[index+1:] > ... def __str__(self): > ... r

Re: str and __setitem__

2007-01-25 Thread Steven D'Aprano
On Thu, 25 Jan 2007 10:16:31 +, Tor Erik Soenvisen wrote: > Hi, > > What do I need to do to make the code below work as expected: Use another language *wink* Python strings are immutable, you can't change them in place. > class str2(str): > > def __setitem__(self, i, y): >

Re: str and __setitem__

2007-01-25 Thread Peter Otten
Tor Erik Soenvisen wrote: > What do I need to do to make the code below work as expected: > > class str2(str): > > def __setitem__(self, i, y): > assert type(y) is str > assert type(i) is int > assert i < len(self) > > self

str and __setitem__

2007-01-25 Thread Tor Erik Soenvisen
Hi, What do I need to do to make the code below work as expected: class str2(str): def __setitem__(self, i, y): assert type(y) is str assert type(i) is int assert i < len(self) self = self[:i] + y + self[1+i:] a = str2('1