python - python3中的operator.idiv在哪里?

标签 python python-3.x

python2.7 中的函数 operator.idiv 似乎在 python 3.4 中不存在。这个功能是不是没有了或者现在在哪里可以找到?

import operator
operator.idiv
>>> AttributeError: module 'operator' has no attribute 'idiv'

最佳答案

在 Python 3 中,根据 PEP 238 ,默认的除法运算符 / 被修改为始终返回 float 结果,无论输入类型如何。因此,虽然在 Python 2 中 3/23.0/2 会返回不同的结果,但您现在在 Python 3 中会得到一致的 float 结果:

>>> 3 / 2
1.5
>>> 3.0 / 2
1.5
>>> 3 / 2.0
1.5

另一个除法运算符是 floor 除法 //,它对结果进行除法(截断小数位)。请注意,此运算符仍然尊重输入类型,因此将它与两个 int 一起使用会得到一个 int,而使用 float 也会将结果变成 float:

>>> 3 // 2
1
>>> 3 // 2.0
1.0
>>> 3.0 // 2
1.0

所以答案是,是的,“经典”除法运算符在 Python 3 中消失了。只有一个“真正的”除法(导致 float )和一个底除法,可以使用 operator.itruedivoperator.ifloordiv 分别。

关于python - python3中的operator.idiv在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37600171/

相关文章:

python - 从 Unicode 字符串中正确提取表情符号

python - 在 python 中训练随机森林时,哪种 dtype 表现更好?

python - 更改具有重复列标题的数据框列中的数据类型

python-3.x - 我应该使用 == 进行字符串比较吗?

python - 关系数据库模型中的Django POST方法

python - Python 实现中的 Sha-3

python Selenium : Send keys is too slow

python循环requests.get()只返回第一个循环

python-3.x - 输入 0 与图层 flatten_5 : expected min_ndim=3, 发现 ndim=2 不兼容

python - PyQt QThreadPool运行函数如何传递参数