I was surprised by the following behavior:

    class MyStr(str):
        def __getitem__(self, key):
            if isinstance(key, slice) and key.start is key.stop is key.end:
                return self
            return type(self)(super().__getitem__(key))

    my_foo = MyStr("foo")
    MY_FOO = MyStr("FOO")
    My_Foo = MyStr("Foo")
    empty = MyStr("")

    assert type(my_foo.casefold()) is str
    assert type(MY_FOO.capitalize()) is str
    assert type(my_foo.center(3)) is str
    assert type(my_foo.expandtabs()) is str
    assert type(my_foo.join(())) is str
    assert type(my_foo.ljust(3)) is str
    assert type(my_foo.lower()) is str
    assert type(my_foo.lstrip()) is str
    assert type(my_foo.replace("x", "y")) is str
    assert type(my_foo.split()[0]) is str
    assert type(my_foo.splitlines()[0]) is str
    assert type(my_foo.strip()) is str
    assert type(empty.swapcase()) is str
    assert type(My_Foo.title()) is str
    assert type(MY_FOO.upper()) is str
    assert type(my_foo.zfill(3)) is str

    assert type(my_foo.partition("z")[0]) is MyStr
    assert type(my_foo.format()) is MyStr

I was under the impression that all of the ``str`` methods exclusively returned 
base ``str`` objects. Is there any reason why those two are different, and is 
there a reason that would apply to ``removeprefix`` and ``removesuffix`` as 
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
Message archived at 
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to