[Python-Dev] Re: PEP 616 -- String methods to remove prefixes and suffixes

2020年3月26日 17:23:21 -0700

> I don't understand that list bit -- surely, if I'm bothering to implement
> removeprefix and removesuffix in my subclass, I would also want
> to
> return self to keep my subclass? Why would I want to go through the extra
> overhead of either calling my own __getitem__ method, or have the
> str.__getitem__ method discard my subclass?
I should clarify: by "when working with subclasses" I meant "when
str.removeprefix() is called on a subclass that does not override
removeprefix", and in that case it should return a base str. I was
not taking a stance on how the methods should be overridden, and
I'm not sure there are many use cases where it should be.
> However, if you are saying that self[:] will call
> self.__class__.__getitem__
> so my subclass only has to override __getitem__ instead of
> removeprefix and
> removesuffix, that I can be happy with.
I was only saying that the new methods should match 20 other methods in 
the str API by always returning a base str (the exceptions being format,
format_map, and (r)partition for some reason). I did not mean to suggest
that they should ever call user-supplied ``__getitem__`` code -- I don't
think they need to. I haven't found anyone trying to use ``str`` as a
mixin class/ABC, and it seems that this would be very difficult to do
given that none of its methods currently rely on 
``self.__class__.__getitem__``.
If ``return self[:]`` in the PEP is too closely linked to "must call 
user-supplied ``__getitem__`` methods" for it not to be true, and so you're
suggesting ``return self`` is more faithful, I can understand.
So now if I understand the dilemma up to this point we have:
Benefits of writing ``return self`` in the PEP:
 - Makes it clear that the optimization of not copying is allowed
 - Makes it clear that ``self.__class__.__getitem__`` isn't used
Benefits of writing ``return self[:]`` in the PEP:
 - Makes it clear that returning self is an implementation detail
 - For subclasses not overriding ``__getitem__`` (the majority of cases), 
makes 
 it clear that this method will return a base str like the other str 
methods.
Did I miss anything?
All the best,
Dennis
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/EQVVXMC7XQJSQIHEB7ND2OLWBQLC7QYM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to