简体   繁体   English

mypy:为什么“int”是“float”的子类型?

[英]mypy: Why is "int" a subtype of "float"?

Why does "mypy" consider "int" as a subtype of "float"?为什么“mypy”将“int”视为“float”的子类型? A subtype shall support all methods of its supertype, but "float" has methods, which "int" does not support:子类型应支持其超类型的所有方法,但“float”具有“int”不支持的方法:

test.py:测试.py:

def f(x : float) -> bool:
    return x.is_integer()

print(f(123.0))
print(f(123))

The static type checker accepts passing an "int" argument for a "float" parameter:静态类型检查器接受为“float”参数传递“int”参数:

(3.8.1) myhost% mypy test.py
Success: no issues found in 1 source file

But this does not guarantee, that there are no errors at runtime:但这并不能保证在运行时没有错误:

(3.8.1) myhost% python test.py
True
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    print(f(123))
  File "test.py", line 2, in f
    return x.is_integer()
AttributeError: 'int' object has no attribute 'is_integer'

because "float" has additional methods, which "int" does not have.因为“float”有额外的方法,而“int”没有。

'Why does "mypy" consider "int" as a subtype of "float"?' “为什么“mypy”将“int”视为“float”的子类型?

Because practicality has so far been considered to beat purity here.因为到目前为止,实用性被认为在这里击败了纯度。 This is not to say that one could not propose that typing define a Scalar type that would include ints and floats but only be valid for arithmetic operations.这并不是说人们不能建议打字定义一个标量类型,它包括整数和浮点数,但只对算术运算有效。

Note that int / int was changed in 3.0 so that float(int / int) == float(int) / float(int), to make int and float arithmetic consistent for equal int and float values.请注意,int / int 在 3.0 中已更改,因此 float(int / int) == float(int) / float(int),以使 int 和 float 算术对于相等的 int 和 float 值保持一致。

Note also that a type-check passing does not mean no runtime errors: division by zero and overflow are still possible, as well as many others.另请注意,类型检查通过并不意味着没有运行时错误:除以零和溢出仍然可能,以及许多其他错误。

As @juanpa.arrivillaga pointed out, the explanation is on https://mypy.readthedocs.io/en/latest/duck_type_compatibility.html .正如@juanpa.arrivillaga指出的,解释在https://mypy.readthedocs.io/en/latest/duck_type_compatibility.html 上

A subtype shall support all methods of its supertype, but "float" has methods, which "int" does not support子类型应支持其超类型的所有方法,但“float”具有“int”不支持的方法

int is not a subtype of float , so it doesn't have to support methods of float . int不是float的子类型,因此它不必支持float方法。

The mechanism is good because passing integer values shouldn't cause errors, unless you really want them as in your example.该机制很好,因为传递整数值不应该导致错误,除非您真的像示例中那样需要它们。 You explicitly tried to use a method which doesn't exist.您明确尝试使用不存在的方法。 In common situations, we only make arithmetic operations on numbers, so a problem rarely exists and you can always avoid it by adding .0 as you wrote.通常情况下,我们只对数字进行算术运算,因此很少存在问题,您可以通过在编写时添加.0来避免它。

It is a common behavior in most languages to assume that int is a special case of float , consider for example C++ int to float implicit conversion.在大多数语言中,假设intfloat的特殊情况是一种常见行为,例如考虑 C++ intfloat隐式转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM