This post is the summary of some pitfalls of Python programming I have encountered. May it help:)
1. remove()
List’s remove function is used to remove certain item from the list. However, remove function always removes the first item it encounters. If we want to remove an item with certain index, then remove function may introduce a bug:
>>> aList = [0, 1, 2, 1]
>>> index = 3
>>> aList.remove(aList[index])
>>> aList
[0, 2, 1]
>>>
Remove function does remove the item 1, but with index of 1 instead of 3. In this case, to remove certain item within certain index, please use pop():
>>> aList = [0, 1, 2, 1]
>>> index = 3
>>> aList.pop(index)
1
>>> aList
[0, 1, 2]
>>>
2. Recursion Limit
RuntimeError: maximum recursion depth exceeded
The default recursion/stack depth limit is 1000 by default. It is easily to exceed this limitation when you are programming for AI/Machine Learning things. When this happens, enlarge the limitation:
sys.setrecursionlimit(20000)
3. sign()
If you wonder if there was/is/will be a sign function in Python, check the argument about sign function here: http://stackoverflow.com/questions/1986152/why-python-doesnt-have-a-sign-function The key point here is to use copysign instead of sign
math.copysign(1, aNumber) == math.sign(aNumber)