假设我们定义一个函数如下:
def multiply(a, b):
return a * b
Run Code Online (Sandbox Code Playgroud)
通过传递数字来调用它显然是有效的:
In [5]: multiply(2,3)
Out[5]: 6
Run Code Online (Sandbox Code Playgroud)
但这(令人惊讶地)知道Python是一种强类型的语言,同样也可以工作:
In [6]: multiply('2', 3)
Out[6]: '222'
Run Code Online (Sandbox Code Playgroud)
或这个
In [7]: multiply(3, '2')
Out[7]: '222'
Run Code Online (Sandbox Code Playgroud)
隐式类型转换让我非常害怕。str类型设计决策背后的原理是什么?另一方面,在F#中,它是不允许的:
- '3' * 2;;
'3' * 2;;
------^
/Users/Pac/stdin(14,7): error FS0001: The type 'int' does not match the type 'char'
Run Code Online (Sandbox Code Playgroud)