[Python-ideas] New explicit methods to trim strings

Paul Moore p.f.moore at gmail.com
Mon Apr 1 03:20:03 EDT 2019


On Mon, 1 Apr 2019 at 01:01, Steven D'Aprano <steve at pearwood.info> wrote:
>
> On Mon, Apr 01, 2019 at 12:02:25PM +1300, Greg Ewing wrote:
> > Dan Sommers wrote:
> >
> > >without_prefix
> > >without_suffix
> > >
> > >They're a little longer, but IMO "without" helps
> > >reenforce the immutability of the underlying string.
> >
> > We don't seem to worry about that distinction for other
> > string methods, such as lstrip and rstrip.
>
> Perhaps we ought to. In the spirit of today's date, let me propose
> renaming existing string methods to be more explicit, e.g.:
>
> str.new_string_in_uppercase
> str.new_string_with_substrings_replaced
> str.new_string_filled_to_the_given_length_with_zeroes_on_the_left
> str.new_string_with_character_translations_not_natural_language_translations
>
> The best thing is that there will no longer be any confusion as to
> whether you are looking at a Unicode string or a byte-string:
>
>     a = a.new_string_trimmed_on_the_left()
>     a = a.new_bytes_trimmed_on_the_left()
>
> *wink*

In order to support duck typing can I suggest that we also add

    bytes.new_string_trimmed_on_the_left()
    str.new_bytes_trimmed_on_the_left()

These will do the obvious thing, so they do not need documenting.
Obviously only needed for Python 3.x, as in 2.x people never use
variables that may sometimes be strings and sometimes bytes.

*wink* (although the joke is obvious, and so should not need documenting :-P)

Paul


More information about the Python-ideas mailing list