Moshe Zadka has been writing a mostly excellent exploration of the Zen of Python for Opensource.com. Not sure I approve of this take on guessing, from The importance of consistency in your Python code:
What should the result of
1 + "1"be? Both
2would be valid guesses. This expression is ambiguous: there is no single thing it can do that would not be a surprise to at least some people.
"11". In Perl, the result is 2. In
I can’t speak for the other languages, but Perl isn’t guessing. It’s
adding these two values as numbers because we told it to by using the
+ operator. If we wanted a string, we would tell Perl, by
. concatenation operator.
Let’s fire up tinyrepl for a quick demonstration.
$ tinyrepl re.pl$ 1 + "1" 2 re.pl$ 1 . "1" 11
1 + "1" and
1 . "1" better than
1 + int("1") and
str(1) + "1"? I don’t know. Perl was born for text processing. Most of its
affordances make the most
sense in that context.
DWIM can be surprising to those unfamiliar with this approach. But it’s not guessing.
The points about consistency and ambiguity are solid. The Zen of Python can be applied with slight modifications to all programming. Just remember that “obvious” is different in different languages.