由於我剛從C++切換到Python,我覺得python並不太在乎類型安全。例如,任何人都可以向我解釋爲什麼在Python中檢查函數參數的類型是不必要的?python:檢查函數的參數類型
說我定義了一個向量類,如下所示:
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??
現在我想做兩個向量之間的點積,所以我再添功能:
def dot(self,other):
# shouldn't I check the two vectors have the same dimension first??
....
這裏是如何做到這一點的答案http://stackoverflow.com/questions/378927/what-is-the-best-idiomatic-way-to-check-the-type-of-a-python-變量 – fceruti