"Occasionally surprise the novice, or you'll continually surprise the expert."
nntp.perl.org
nntp.perl.org
>>> a, b = 123, 456
>>> a + b
579
>>> c, d = '123', '456'
>>> c + d
'123456'
>>> b + c
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
b + c
TypeError: unsupported operand type(s) for +: 'int' and 'str'
I must confess that none of these behaviours surprised me when I first encountered them.What a silly error. Obviously "123" + 1 is "23", "123" + 2 is "3", "123" + 3 is "", and "123" + 456 is "ùÏÚ0óḚ̂qÛ=æPªuök6~;;\ðÚÕKµM¦OñÁ®"
I think one reason is that python encourages the use of duck typing, and as a result when you look at a piece of code you can't really be positive what kinds of values the variables hold. So if you're writing some code that works as long as the arguments passed in support "+", it's jarring thinking that it might run on strings, but surely won't do what you expect.
"I wanted to minimize my frustration during programming, so I want to minimize my effort in programming. That was my primary goal in designing Ruby. I want to have fun in programming myself. After releasing Ruby and many people around the world got to know Ruby, they said they feel the way I feel. They came up with the phrase the principle of least surprise. But actually, it's often misunderstood.
...
Everyone has an individual background. Someone may come from Python, someone else may come from Perl, and they may be surprised by different aspects of the language. Then they come up to me and say, "I was surprised by this feature of the language, so therefore Ruby violates the principle of least surprise." Wait. Wait. The principle of least surprise is not for you only. The principle of least surprise means principle of least my surprise. And it means the principle of least surprise after you learn Ruby very well. For example, I was a C++ programmer before I started designing Ruby. I programmed in C++ exclusively for two or three years. And after two years of C++ programming, it still surprised me."
Also, isn't it instructive that the "novice" here is someone who's already a programmer in another language? Why are the arbitrary conventions of the Unix shell sacred? They are not some ultimate good. They are merely good because they are the result of cooperation and collaboration over many years. They are by no means perfect.
But anyway, what about $_ and a host of other special variables? Pulling arguments to functions out of nowhere? Using regexps for simple string operations? Strings not being array? Using a special comparison operator for strings (without complaints when == is used)? length() not working on arrays (again, no warning) and $# returning index of last element rather than length? 'if' not being able to take a single statement? 'Last' instead of 'break', 'next' instead of 'continue', 'continue' doing something else?