On 2010-09-26, Steven D'Aprano <st...@remove-this-cybersource.com.au> wrote: > On Sat, 25 Sep 2010 23:46:57 -0700, Paul Rubin wrote: >> I think the idea is you should not be able to do mathematical operations >> on strings, and if you try to do one, Python should raise an exception, >> rather than using hokey analogies to guess at what you were trying to >> do. If you want to replicate a sequence, introduce a function with a >> name like "replicate", rather than overloading an arithmetic operator.
> But * isn't a mathematical operation on sequences. It's a repetition > operator. I think Paul's point is that "*" is in general a mathematical operation, and discovering that for some types of objects, it's been given a fundamentally unrelated meaning can certainly be a bit of a surprise. I actually sort of like that perl has a separate repetition operator. And really, when you find yourself saying something nice about perl's syntax or punctuation, that's always scary territory to be in. > There's nothing obscure or unintuitive about "spam"*3 = "spamspamspam", > and the fact that it doesn't do the same thing as int("spam")*3 is a > foolish argument. The languages in which it's surprising are mostly things like perl, where there's a certain amount of implicit coercion going on, so it's ambiguous whether "3"*3 ought to mean "333" or 9. (Except it isn't, because repetition is a separate operator, written "3" x 3 rather than "3" * 3...) People coming from that kind of background may be expecting * to stay an arithmetic operator, rather than having it change form when applied to non-arithmetic objects. I'm not sure either way. I think on the whole, I like the notion of a repetition operator which is distinct from multiplication, but I also definitely prefer the lack of implicit coercion, and without implicit coercion, there's a lot less underlying ambiguity to worry about. >From the top, I guess my analysis is: * It seems clear that, given two sequences x and y, "x + y" ought to be the concatenation of these sequences. * Thus, "x + x" should be x concatenated to itself. * Idiomatically, it is not unreasonable to assert that "x * 2" and "x + x" could be the same value. * It thus makes some sense for <sequence> * <number> to indicate repetition of the sequence. * Since a string is a kind of a sequence, it also makes sense for <string> * <number> to indicate repetition. So I think I'm pretty much convinced that Python's behavior makes sense, but it makes sense only because I'm going into this expecting operators to be defined by types, so I don't expect every '*' to mean 'multiplication'. Helps, perhaps, that I got exposed to group theory early enough to be used to redefining + and * to be any two operations which have interesting properties*. -s [*] What's purple and commutes? An abelian grape. -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nos...@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! I am not speaking for my employer, although they do rent some of my opinions. -- http://mail.python.org/mailman/listinfo/python-list