Is Python type safe?

后端 未结 8 1761
猫巷女王i
猫巷女王i 2021-01-30 17:14

According to Wikipedia

Computer scientists consider a language \"type-safe\" if it does not allow operations or conversions that violate the rules of the

8条回答
  •  不知归路
    2021-01-30 17:24

    In Python, you'll get a runtime error if you use a variable from the wrong type in the wrong context. E.g.:

    >>> 'a' + 1
    
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: cannot concatenate 'str' and 'int' objects
    

    Since this check only happens in runtime, and not before you run the program, Python is not a typesafe language (PEP-484 notwithstanding).

提交回复
热议问题