[Python-ideas] New explicit methods to trim strings

Jonathan Fine jfine2358 at gmail.com
Mon Mar 25 06:22:53 EDT 2019


Instead of naming these operations, we could use '+' and '-', with
semantics:

    # Set the values of the variables.
    >>> a = 'hello '
    >>> b = 'world'
    >>> c = 'hello world'

    # Some values between the variables.
    >>> a + b == c
    True
    >>> a == c - b
    True
    >>> b = -a + c
    True

   # Just like numbers except.
    >>> a + b == b + a
    False

This approach has both attractions and problems. And also decisions. The
main issue, I think come to this. Suppose we have
     a, A = ('a', -'a')
     b, B = ('b', -'b')
     a + A == A + a  == ''
     b + B == B + b == ''
     A + '' == '' + A == A
     B + '' == '' + B == B
together with unrestricted addition of a, A, b, B then we have what
mathematicians call the free group on 2 letters, which is an enormous
object. If you want the math, look at,
https://en.wikipedia.org/wiki/Free_group#Examples

We've made a big mistake, I think, if we allow Python programmers to
accidentally encounter this free group. One way to look at this, is that we
want to cut the free group down to a useful size. One way is
   >>> 'hello ' - 'world' == 'hello'   # I like to call this truncation.
   True
Another way is
    >>> 'hello' - 'world'  # I like to call this subtraction.
   ValueError: string s1 does not end with s2, so can't be subtracted

I hope this little discussion helps with naming things. I think this is
enough for now.

-- 
Jonathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190325/b3fc599b/attachment.html>


More information about the Python-ideas mailing list