Today I was bitten again by mutable default arguments after many years. I usually don't use mutable default arguments unless needed, but I think with time I forgot about that. Today in the application I added tocElements=[] in a PDF generation function's argument list and now "Table of Contents" gets longer and longer after each invocation of "generate pdf". :)
What else should I add to my list of things to MUST avoid?
Always import modules the same way, e.g.
from y import xandimport xare treated as different modules.Do not use range in place of lists because
range()will become an iterator anyway, the following will fail:myIndexList = [0, 1, 3] isListSorted = myIndexList == range(3) # will fail in 3.0 isListSorted = myIndexList == list(range(3)) # will notSame thing can be mistakenly done with xrange:
myIndexList == xrange(3)Be careful catching multiple exception types:
try: raise KeyError("hmm bug") except KeyError, TypeError: print TypeErrorThis prints "hmm bug", though it is not a bug; it looks like we are catching exceptions of both types, but instead we are catching KeyError only as variable TypeError, use this instead:
try: raise KeyError("hmm bug") except (KeyError, TypeError): print TypeError