On 9/26/2010 1:16 AM, Paul Rubin wrote:
Steven D'Aprano<st...@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"?
Should "spam"*2.5 be "spamspamsp"?
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...)).
I say it's all hokey from the get-go ;-).
I tend to agree. "+" as concatenation is at least associative.
Trying to concatenate a string with an integer raises an exception,
so that's not going to generate unexpected values.
"*" as repetition, on the other hand, is a mixed-mode operation.
That's why this is troublesome. Where the type spaces are isolated,
overloading isn't so risky.
We should have something like "repeat" or "dup" as
a string method rather than an overloading of the "*" operator.
Strings already have a range of methods which perform basic
operations on the string and return a new string, so
that's consistent.
"xyz".dup(3)
is clear enough.
And allowing
"xyz"*-3
was just lame. What reasonable use case does that have?
John Nagle
--
http://mail.python.org/mailman/listinfo/python-list