[Tutor] MutableString/Class variables

Albert-Jan Roskam fomcl at yahoo.com
Fri May 10 16:56:19 CEST 2013



> From: Steven D'Aprano <steve at pearwood.info>

> To: tutor at python.org
> Cc: 
> Sent: Friday, May 10, 2013 1:28 PM
> Subject: Re: [Tutor] MutableString/Class variables
> 
> On 09/05/13 22:10, Albert-Jan Roskam wrote:
>>  Hello,
>> 
>>  I was just playing a bit with Python and I wanted to make a mutable string, 
> that supports item assignment. Is the way below the way to do this?
> 
> 
> Guido's time machine strikes again:
> 
> py> from UserString import MutableString
> py> s = MutableString("Hello world!")
> py> s[-1] = '?'
> py> print s
> Hello world?
> 
> 
> You can see the source code here:
> 
> http://hg.python.org/cpython/file/2.7/Lib/UserString.py
> 
> and the docs:
> 
> http://docs.python.org/2/library/userdict.html#module-UserString
> 
> 
> Note, however, that MutableString is (allegedly) very inefficient, and has been 
> removed from Python 3.x. (It seems to me that it cannot possibly be that much 
> more inefficient than using immutable strings. It's just a wrapper around an 
> immutable string with a few convenience methods.)
> 
> But really, unless you are dealing with truly humongous strings, you're 
> better off just sticking to the standard Python built-in string (unicode) type. 
> And if by chance you really do need a mutable string-like data structure, you 
> probably should look at something like ropes, implemented in C or Cython.
> 
> http://en.wikipedia.org/wiki/Rope_(data_structure)

Hi Steven,

"[...] the purpose of this class is an educational one: to prevent people from inventing their own mutable string class"
Waaaahh, twilight zone! ;-)))
Still interesting to see how the author implemented __setitem__. In my version, slices were not supported.
Interesting to hear about ropes. I did not know about this. Here is another page where a Python implementation of a rope data structure is mentioned: http://kmike.ru/python-data-structures/#ropes

Regards,
Albert-Jan

 
> 
> 
> -- 
> Steven
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list