WTFPython: Exploring and understanding Python through surprising snippets
There are some genuinely surprising ones, but is it just me who seems to think these are for 80% either well-known beginner's mistakes (== vs. is, underscored names) or just begging for problems with operator precedence (by omitting parentheses) and variable scoping (by shadowing variables)?<p>Random example I'm currently reading is: x, y = (0, 1) if True else None, None. The interpreter considers the last None outside the if statement. Gee, so you thought to need parentheses at first but not for the second one, and now it's a pitfall that parentheses are missing? The readme goes so far as so say "I haven't met even a single experience Pythonist till date who has not come across one or more of the following scenarios". Uh huh. Not me.<p>Add parentheses when in doubt, and when not in doubt, add parentheses for readability. In any language. Not excessively, like everyone knows what "if x == 2:" does without them in python, but for anything less obvious or more complicated than a=2*2+3, just do it.