regex into str

Jeremy Bowers jerf at jerf.org
Mon Aug 30 06:49:40 CEST 2004


On Sun, 29 Aug 2004 12:58:57 +0200, Peter Kleiweg wrote:

> That is not relevant. What matters is that a += 'b' does what it
> is supposed to do.

If you want to be that way, then fine. No, it doesn't do what it is
"supposed" to do, in the context you are discussing. It does not add a "b"
to the original string, it creates a new string containing the original
contents plus the new contents.

Let me refresh your memory: You are arguing that you should be able to
apply a division operator to a string to apply a regex to it. When people
told you it was impossible, because strings were immutable, you said that
a += "b" did what you wanted. In context, this was clearly a claim that
strings are mutable, although that is a translation of your claim from
what you said here:

  This works:

      a += 'x'

  So this should too:

      a /= 'x'

which was in reply to 

> Even if you could assign to str.__div__ (and this is very deliberately and
> specifically disallowed) you would end up disappointed, because strings
> are immutable.  That means there's nowhere to store "the last match",
> no way to mutate the string with the "/=" operator, and also that the
> interpreter is free to use the same storage for two equal strings.

Therefore, I say again: Your example does not do what you are implicitly
claiming it does. Therefore it is not a counter example to Jeff Epler's
(correct) claim. 

So when you say above "This works:", I'm saying, no, it doesn't, not in
the sense you were replying to Jeff. It is not mutating the string
originally referenced by a, you still can't do that, and your
attempted counterpoint has no force, no meaning. (Any other putative
meaning you would claim it had after the fact would simply be a
non-sequitor, in context.)

Any successful attempt to mutate a string (in pure Python) would
constitute a serious bug in Python. (Any successful mutation by a C
extension would constitute a major, Python-breaking bug in that extension.)




More information about the Python-list mailing list