Re:A few things to remember while coding in Python
by makecheck on Sunday May 20, 2012 @02:43AM
(#3998145)
Well that's true, a function is generally written as if it's been given what it wants. I wouldn't check for other types either.
But None is a result that can happen in situations that would otherwise return exactly the expected type. If "nothingness" can be meaningful (especially in a function that accepts an empty list as a parameter, say), it's nicer if the code just deals with None itself instead of requiring checks for None in all the callers.
In the expensive case, I'd just calculate it once and store it somewhere (possibly as a lookup dictionary if there are multiple inputs) and access that from within the function.