is explicit better than implicit?
The second line of the Zen of Python reads, "explicit is better than implicit," and while I agree with the aphorisms of the Zen of Python in general, I find that this one in particular goes not only against the Zen of Python itself, but is also the aphorism most frequently touted to quelch an argument about improving capabilities of a given system.
The aphorism does apply to some very good examples. Consider parameter passing to a function.
It's pretty clear from the name of the function and the string argument passed in that the function will likely print out the string, but it's quite unclear what the purpose of the second parameter is. I've intentionally not defined it previously to exaggerate the point.
This ambiguity can be lessened by the caller by explicitly including the name of the second parameter.
And indeed, that's the canonical example that illustrates how Python's ability to be explicit about parameter names improves the potential readability of code over other languages including C++, Java, and C#.
But when the aphorism "explicit is better than implicit" is treated instead like a guiding principle, it misleads. It generally purports that the programmer should do more work in the name of clarity. C#, Java, C++, and assembly, for that matter, are much more explicit than Python. It's the duck-typing and high-level abstraction and dynamic nature of Python that gives it all its power and attraction. It's the use of previously-implicit white space to designate syntactical delimiters that removes the need for the explicit line terminators and logical block delimiters.
The ability to introspect a class or method or function and use its name instead of having to explicitly define the same string of characters as a member means less clutter and more consistency. When an ORM can dynamically create classes to match the names of tables in a database (without having to generate any code), this is a powerful construct.
All of these examples go against the aphorism. This contradiction is easily satirized.
So the question I pose, dear reader: Does "explicit is better than implicit" have more clearly beneficial examples, or is maybe this aphorism ready for the dustbin?
The aphorism does apply to some very good examples. Consider parameter passing to a function.
print_string("my dog has fleas", True)
It's pretty clear from the name of the function and the string argument passed in that the function will likely print out the string, but it's quite unclear what the purpose of the second parameter is. I've intentionally not defined it previously to exaggerate the point.
This ambiguity can be lessened by the caller by explicitly including the name of the second parameter.
print_string("my dog has fleas", append_newline=True)
And indeed, that's the canonical example that illustrates how Python's ability to be explicit about parameter names improves the potential readability of code over other languages including C++, Java, and C#.
But when the aphorism "explicit is better than implicit" is treated instead like a guiding principle, it misleads. It generally purports that the programmer should do more work in the name of clarity. C#, Java, C++, and assembly, for that matter, are much more explicit than Python. It's the duck-typing and high-level abstraction and dynamic nature of Python that gives it all its power and attraction. It's the use of previously-implicit white space to designate syntactical delimiters that removes the need for the explicit line terminators and logical block delimiters.
The ability to introspect a class or method or function and use its name instead of having to explicitly define the same string of characters as a member means less clutter and more consistency. When an ORM can dynamically create classes to match the names of tables in a database (without having to generate any code), this is a powerful construct.
All of these examples go against the aphorism. This contradiction is easily satirized.
So the question I pose, dear reader: Does "explicit is better than implicit" have more clearly beneficial examples, or is maybe this aphorism ready for the dustbin?
Written on June 12, 2009