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

2020年3月24日 13:20:25 -0700

I think my confusion is about just how precise this sort of "reference 
implementation" should be. Should it behave with ``str`` and ``tuple`` 
subclasses exactly how it would when implemented? If so, I would expect the 
following to work:
 class S(str): __len__ = __getitem__ = __iter__ = None
 class T(tuple): __len__ = __getitem__ = __iter__ = None
 x = str.cutprefix("FooBar", T(("a", S("Foo"), 17)))
 assert x == "Bar"
 assert type(x) is str
and so I think the ``str.__getitem__(self, slice(str.__len__(prefix), None))`` 
monstrosity would be the most technically correct, unless I'm missing 
something. But I've never seen Python code so ugly. And I suppose this is a 
slippery slope -- should it also guard against people redefining ``len = lambda 
x: 5`` and ``str = list`` in the global scope? Clearly not. I think then maybe 
it would be preferred to use the something like the following in the PEP:
 def cutprefix(self, prefix, /):
 if isinstance(prefix, str):
 if self.startswith(prefix):
 return self[len(prefix):]
 return self[:]
 elif isinstance(prefix, tuple):
 for option in prefix:
 if self.startswith(option):
 return self[len(option):] 
 return self[:]
 else:
 raise TypeError()
 def cutsuffix(self, suffix):
 if isinstance(suffix, str):
 if self.endswith(suffix):
 return self[:len(self)-len(suffix)]
 return self[:]
 elif isinstance(suffix, tuple):
 for option in suffix:
 if self.endswith(option):
 return self[:len(self)-len(option)] 
 return self[:]
 else:
 raise TypeError()
The above would fail the assertions as written before, but would pass them for 
subclasses ``class S(str): pass`` and ``class T(tuple): pass`` that do not 
override any dunder methods. Is this an acceptable compromise if it appears 
alongside a clarifying sentence like the following?
 These methods should always return base ``str`` objects, even when called 
on ``str`` subclasses.
I'm looking for guidance as to whether that's an appropriate level of precision 
for a PEP. If so, I'll make that change.
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/PV6ANJL7KN4VHPSNPZSAZGQCEWHEKYG2/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to