sequence multiplied by -1
Steven D'Aprano
steve-REMOVE-THIS at cybersource.com.au
Mon Sep 27 00:46:30 EDT 2010
On Sun, 26 Sep 2010 01:16:49 -0700, Paul Rubin wrote:
> Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
>> There's nothing obscure or unintuitive about "spam"*3 = "spamspamspam",
> Why would it not be ["spam","spam","spam"] or even "ssspppaaammm"?
The first one would be a reasonable design choice, although less useful
than what Python does. Instead of:
rule = "="*12
you would need to write:
rule = ''.join("="*12)
which is a pain.
The second one is just a lousy design. If you ask for three sheep, you
expect three sheep, not three sheep heads, plus three sheep tails, plus
three sheep left-front legs, plus...
"ssspppaaammm" fails the Principle of Least Surprise. For any positive n,
you would expect:
"spam" in ("spam"*n)
to return True. "ssspppaaammm" fails this test.
> Should "spam"*2.5 be "spamspamsp"?
Trying to determine a fraction of a string is troublesome... what is
1/100th of "abcde"? Some fraction of the letter "a"? What does that even
mean? The question has no sensible answer, in the same way that asking
for the real-valued square-root of -1 has no sensible answer. Unlike real
numbers, which are easily extended to complex values, there's no obvious
extension of strings to include fractions of characters.
So the only sensible thing is to say that fractions of strings doesn't
have any meaning. When faced with "spam"*2.5, you can either raise an
exception (as Python does), or truncate the second argument to an integer
value. Which you do is a matter of taste, although I'd argue in favour of
truncation.
> Should "spam"-"a" be "spm"? What about "spamspam"-"a"? And what about
> "spam"/2? "sp" be an ok first guess, but "sa" might make more sense (it
> means (1,2,3,...)/2 would be (1,3,5...)).
What makes you think that "spam" - "a" should be anything at all?
Just because an operator exists doesn't mean it *has* to be defined for
every possible data set. You're over-thinking this. Repetition of
sequences is simple and straight-forward. Trying to come up with
complicated analogues to arbitrary real-number operators is a waste of
time and effort. That is exactly the sort of nonsense that I already
agreed should be avoided.
--
Steven
More information about the Python-list
mailing list