[Python-ideas] String Subtraction
Mike Meyer
mwm-keyword-python.b4bdba at mired.org
Thu Oct 14 23:23:11 CEST 2010
On Thu, 14 Oct 2010 20:58:52 +0000
Dave Jakeman <davejakeman at hotmail.com> wrote:
> I'm new to Python and this is my first suggestion, so please bear with me:
>
> I believe there is a simple but useful string operation missing from Python: subtraction. This is best described by way of example:
>
> >>> "Mr Nesbit has learnt the first lesson of not being seen." - "Nesbit "
> 'Mr has learnt the first lesson of not being seen.'
> >>> "Can't have egg, bacon, spam and sausage without the spam." - " spam"
> 'Can't have egg, bacon, and sausage without the spam.'
> >>> "I'll bite your legs off!" - "arms"
> 'I'll bite your legs off!'
>
> If b and c were strings, then:
>
> a = b - c
>
> would be equivalent to:
The existing construct a = b.replace(c, '', 1)
The problem isn't that it's non-intuitive (there's only one intuitive
interface, and it's got nothing to do with computers), it's that there
are a wealth of "intuitive" meanings. A case can be made that it
should mean the same as any of thise:
a = b.replace(c, '')
a = b.replace(c, ' ', 1)
a = b.replace(c, ' ')
For that matter, it might also mean the same thing as any of these:
a = re.sub(r'\s*%s\s*' % c, '', b, 1)
a = re.sub(r'\s*%s\s*' % c, '', b)
a = re.sub(r'\s*%s\s*' % c, ' ', b, 1)
a = re.sub(r'\s*%s\s*' % c, ' ', b)
a = re.sub(r'%s\s*' % c, '', b, 1)
a = re.sub(r'%s\s*' % c, '', b)
a = re.sub(r'%s\s*' % c, ' ', b, 1)
a = re.sub(r'%s\s*' % c, ' ', b)
a = re.sub(r'\s*%s' % c, '', b, 1)
a = re.sub(r'\s*%s' % c, '', b)
a = re.sub(r'\s*%s' % c, ' ', b, 1)
a = re.sub(r'\s*%s' % c, ' ', b)
Unless you can make a clear case as to why exactly one of those cases
is different enough from the others to warrant a syntax all it's own,
It's probably best to be explicit about the desired behavior.
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
More information about the Python-ideas
mailing list