7 Mar
2020
7 Mar
'20
11:45 p.m.
On Sun, Mar 8, 2020 at 10:10 AM Christopher Barker <pythonchb@gmail.com> wrote:
On Sat, Mar 7, 2020 at 3:01 PM Cameron Simpson <cs@cskk.id.au> wrote:
Like yours, they return the original object if unchanged.
that makes me uncomfortable, but I guess as srings are mutable (an may be interned, why not?)
Do the other string methods return themselves if there is no change?
Yes. There's no reason not to; it's more efficient to return the same string, and perfectly safe to do so. Not every method returns itself when there's no change, but when it's easy to do, they do:
x = "hello world, this is a test" x.strip("@") is x True x.replace("@", "#") is x True x.zfill(5) is x True x.center(5) is x True
But:
x.lower() is x False x.lower() == x True
ChrisA