andy at wild-flower.co.uk
Sat Sep 20 21:33:22 CEST 2003
On Saturday 20 Sep 2003 6:40 pm, Gordon Airport wrote:
> Has anyone suggested introducing a mutable string type (yes, of course)
> and distinguishing them from standard strings by the quote type - single
> or double? As far as I know ' and " are currently interchangeable in all
> circumstances (as long as they're paired) so there's no overloading to
> muddy the language. Of course there could be some interesting problems
> with current code that doesn't make a distinction, but it would be dead
> easy to fix with a search-and-replace. And which would be the default
> return type for functions returning strings...
> It looks like there are ways of handling this by digging around in the
> modules for more basic types, but it would be much nicer to have it
> available at 'user level'.
Mutable strings are one thing that I missed, initially, when I first started
using Python. After a while, as the "Pythonic" way of doing things sank in,
I realised that Python doesn't *need* mutable strings.
Python strings (and integers and floats) are all immutable for a very good
reason: dictionaries can't reliably use mutable objects as keys. At first,
this seemed rather like "the tail wagging the dog"... however, once I fully
understood the % (percent) string operator, and the ability to efficiently
convert strings into lists and back, my anxiety went away. These cover most
usage of strings that might convince you you need mutability.
As for the suggestion that the kind of quote used should determine whether or
not a string is mutable, I sort of /half/ agree. On one hand, making (say)
the apostrophe mean mutable and the double quote mean immutable would break
thousands of existing applications - for end users, "a simple search and
replace" is simply not feasable! Furthermore, the meaning of the following
snippet would be subtly (and possibly dangerously) changed:
s1="this is an 'immutable' string"
s2='this is a "mutable" string'
s3=s1.replace("'",'"')+" and "+s2.replace('"','"') # replace quotes with
# apostrophes and vice-versa
Q1) What type will s3 be?
Q2) What happens to s2? As it's mutable, shouldn't it do the replacement
Q3) Will the assignment of d1 succeed? If it fails, wouldn't that be
On the other hand, Python already has this type distinction for raw and
unicode strings (r"..." and u"...", respectively). If it were to be adopted,
I would be ok with an m"..." type of string, which could be barred from being
a dictionary key. This would open up a can of worms wrt the other immutable
types, too: would we end up with:
a=1234567m # a mutable integer
b=1234567.89m # a mutable float
c=123456789012345678901234567890Lm # a mutable long integer
d=123+456jm # a mutable complex number
e=m(1,2,3,4,5,6,"a","b","c") # a mutable tuple !!! :-p
This could start a flame-war/heated debate on the scale of the ternary
Maybe there's a project for you (and a good introduction to a practical
application for new-style Python classes to boot)!
I'm sure people have written this type of thing in the past - and in some
situations, it's bound to be useful, but I think it should be kept as a
separate module, so that you have to *declare* your usage of this /strange/
behaviour to the reader; "explicit is better than implicit".
Remember, Mohammed had to go *to* the mountain, not the other way round!
More information about the Python-list