[Python-ideas] Experiment: Adding "re" to string objects.

Nick Coghlan ncoghlan at gmail.com
Tue Jul 21 23:16:56 CEST 2009


Sean Reifschneider wrote:
>> Why is this not mutable?
> 
> I understand that the string.re is mutable, but saying that this "makes
> strings mutable" is kind of misleading...

Because it is blurring the distinction: while having a mutable attribute
that plays no part in equality checks technically preserves the
immutability of the strings themselves, it does mean that there are now
some operations on a string that alter the internal state of that
specific string. Better to have a separate type that is known to have
mutable internal state and hence may not be safe to share between
different operations.

It also just occurred to me that string interning completely destroys
any concept of storing mutable attributes on strings:

.>>> x = "aString"
.>>> y = "aString"
.>>> x is y
True


.>>> x = intern("I am a string")
.>>> y = intern("I am a string")
.>>> x is y
True

You can't make this safe without removing the intern() operation along
with the implicit interning of "identifier-like" strings. And that is
too critical to the normal operation of the language to ever happen.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------



More information about the Python-ideas mailing list