I'm removing the tuple feature from this PEP. So now, if I understand
correctly, I don't think there's disagreement about behavior, just about
how that behavior should be summarized in Python code. 

Ethan Furman wrote:
> > It appears that in CPython, self[:] is self is true for base
> > str
> >  objects, so I think return self[:] is consistent with (1) the premise
> >  that returning self is an implementation detail that is neither mandated
> >  nor forbidden, and (2) the premise that the methods should return base
> >  str objects even when called on str subclasses.
> The Python interpreter in my head sees self[:] and returns a copy. 
> A
> note that says a str is returned would be more useful than trying to
> exactly mirror internal details in the Python "roughly equivalent" code.

I think I'm still in the camp that ``return self[:]`` more precisely prescribes
the desired behavior. It would feel strange to me to write ``return self``
and then say "but you don't actually have to return self, and in fact
you shouldn't when working with subclasses". To me, it feels like

    return (the original object unchanged, or a copy of the object, 
            depending on implementation details, 
            but always make a copy when working with subclasses)

is well-summarized by

   return self[:]

especially if followed by the text

    Note that ``self[:]`` might not actually make a copy -- if the affix
    is empty or not found, and if ``type(self) is str``, then these methods
    may, but are not required to, make the optimization of returning ``self``.
    However, when called on instances of subclasses of ``str``, these
    methods should return base ``str`` objects, not ``self``.

...which is a necessary explanation regardless. Granted, ``return self[:]``
isn't perfect if ``__getitem__`` is overridden, but at the cost of three
characters, the Python gains accuracy over both the optional nature of
returning ``self`` in all cases and the impossibility (assuming no dunders
are overridden) of returning self for subclasses. It also dissuades readers
from relying on the behavior of returning self, which we're specifying is
an implementation detail.

Is that text explanation satisfactory?
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/4E77QD52JCMHSP7O62C57XILLQN6SPCT/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to