Since I just switched to Python from C++, I feel like python does not care much about type safety. For example, can anyone explain to me why checking the types of function parameters is not necessary in Python?
Say I defined a Vector class as follows:
class Vector:
def __init__(self, *args):
# args contains the components of a vector
# shouldn't I check if all the elements contained in args are all numbers??
And now I want to do dot product between two vectors, so I add another function:
def dot(self,other):
# shouldn't I check the two vectors have the same dimension first??
....
Well, as for necessity of checking the types, that may be a topic that’s a bit open, but in python, its considered good form to follow “duck typing”. The function just uses the interfaces it needs, and it’s up to the caller to pass (or not) arguments that properly implement that interface. Depending on just how clever the function is, it may specify just how it uses the interfaces of the arguments it takes.