22-Aug-2004 Long time Pythoneer Tim Peters succinctly channels the BDFL’s guiding principles for Python’s design into 20 aphorisms, only 19 of which have been written down. @myriadbhoom
implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren’t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. @myriadbhoom
-- obvious way to do it. If the implementation is hard to explain, it’s a bad idea. If the implementation is easy to explain, it may be a good idea. Although that way may not be obvious at first unless you’re Dutch. Namespaces are one honking great idea -- let’s do more of those. Now is better than never. Although never is often better than *right* now. @myriadbhoom
|| respectively. Though it is subjective, code seems more readable and beautiful this way. versus if valid(a) and b == 0 or s == 'yes': @myriadbhoom if(valid(a) && b == 0 || s == 'yes') {
= "5"; echo $foo * 3; ?> [$]> php test.php 15% Here’s another example of Explicit is better than implicit, applied to the language itself. While Python is dynamically-typed, it is also strongly-typed. @myriadbhoom This is known as type coercion . You’ll also see it used frequently in C, where programmers often take advantage of the compiler’s lack of caring to put bits in places they don’t belong.
them, however, >>> foo+3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects produces an exception. Tell Python that you want an integer: >>>int(foo)+3 8 Now, in Python, multiplying a string by an integer, will print the string that many times. ExplicitIsBetterThanImplicit-2 @myriadbhoom
def gcd(x, y): while y: x, y = y, x % y return x @myriadbhoom public int gcd(int m, int n) { while (n > 0) { int r = m % n; m = n; n = r; } } Don’t make me think philosophy The Euclid’s algorithm in C and Python shows the beauty of this saying
sqrt(i) elif i==0: return 0 else: return 1j * sqrt(-i) Don’t try to stick too much code on one line versus if i > 0: return sqrt(i) elif i == 0: return 0 else: return 1j * sqrt(-i) @myriadbhoom
int main(void) { printf("Hello, world!\n"); return(0); } And what about indentation ? Well indented code is more readable . Thus, in Python it’s mandatory. versus print "Hello world!" @myriadbhoom
it: if not a and b: do something What binds more tightly ‘not’ or ‘and’ ? The syntax is unambiguous , but my memory is not. Or somewhat ugly but reliable if it does: Could it be (no) ? @myriadbhoom if not(a and b): do something if b and not a: do something if (not a) and b: do something
(and be able to read other people’s code), you must learn them all. In Pyton you must learn only one: How many ways could you provide to “iterate over a sequence “ in C++ ? for element in sequence: And isn’t obvious ? There should be one module for every need. Iterating over an array with integers or pointers; iterating over STL vectors with various kinds of iterators … etc. @myriadbhoom
way to do this, and it isn’t obvious at first. One of the sidebars for this is always the observation that the condition is evaluated first irrespective of the left-to-right order of the Guido van Rossum. The classic trinary if-then-else operator operands. (it’s a = cond?expr1:expr2 in C) was debated hotly. Guido came up with this: a = expr1 if cond else expr2 @myriadbhoom
> 2 is the sum of two primes') assert not 'this sentence is false' f.close() with open('i_will_be_closed_right_away.txt’,'w') as f: f.write('the sum of two primes > 2 is an even number’) raise AssertionError('this sentence is false') @myriadbhoom
of those! Every time you invoke a function import os print os.getcwd() instead of this from os import * print getcwd() @myriadbhoom An example for this case.