[Python-ideas] Complicate str methods

Michel Desmoulin desmoulinmichel at gmail.com
Tue Feb 13 07:49:43 EST 2018



Le 03/02/2018 à 23:04, Franklin? Lee a écrit :
> Let s be a str. I propose to allow these existing str methods to take
> params in new forms.
> 
> s.replace(old, new):
>     Allow passing in a collection of olds.
>     Allow passing in a single argument, a mapping of olds to news.
>     Allow the olds in the mapping to be tuples of strings.
> 
> s.split(sep), s.rsplit, s.partition:
>     Allow sep to be a collection of separators.
> 
> s.startswith, s.endswith:
>     Allow argument to be a collection of strings.
> 
> s.find, s.index, s.count, x in s:
>     Similar.
>     These methods are also in `list`, which can't distinguish between
> items, subsequences, and subsets. However, `str` is already inconsistent
> with `list` here: list.M looks for an item, while str.M looks for a
> subsequence.
> 
> s.[r|l]strip:
>     Sadly, these functions already interpret their str arguments as
> collections of characters.
> 

I second that proposal. I regularly need those and feel frustrated when
it doesn't work.

I even wrote a wrapper that does exactly this :
https://github.com/Tygs/ww/blob/master/src/ww/wrappers/strings.py

But because it's pure Python, it's guaranteed to be slow. Plus you need
to install it every time you need it.


More information about the Python-ideas mailing list