I only use assert if they provide much better diagnostics than the error messages I would otherwise receive. Your third statement
assert(callable(func))
can be an example for such a statement - if func not callable, you will receive an error message in a completely different line of code than in the case of the actual error, and it may be unclear -callable object in self.symbols_map . I write "maybe" because it depends on the rest of your code - if this is the only place where self.symbols_map updated, the statement may also be unnecessary.
The first and last statement, of course, contradict the idea of duck input , and the second is redundant. If symbol not a string of length 1, it is possible that self.symbols_map[symbol] will raise KeyError , so there is no need for statements.
The last statement is also incorrect - type(keypress) cannot be None , and type checks must be performed using isinstance() . There may be very specialized applications where you cannot resolve subtypes, but checking should be done using type(x) is int instead of type(x) == int . The None check should be done using x is None , not type(x) is NoneType .
You should probably write a good set of unit tests - they will be much more useful than statements, and can make almost all of your statements redundant.
source share