On Sun, Mar 8, 2020 at 10:10 AM Christopher Barker <[email protected]> wrote:
>
> On Sat, Mar 7, 2020 at 3:01 PM Cameron Simpson <[email protected]> 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
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/GGYZZNNOGLRDQ74XCKUI7ZSFWYOGBGNH/
Code of Conduct: http://python.org/psf/codeofconduct/