gpt4 book ai didi

python - 检查数字零时的多条件声明 - python

转载 作者:行者123 更新时间:2023-11-28 22:39:22 26 4
gpt4 key购买 nike

关于 Idiomatic Python - checking for zero 有问题但是考虑这个问题还要检查条件内的变量类型。

给定 0 if not variable else variable 样式语句,它将让空对象溜走,例如

>>> x, y = None, []
>>> 0 if not(x and y) else x / y
0
>>> x, y = None, 0
>>> 0 if not(x and y) else x / y
0
>>> x, y = 0, 1
>>> 0 if not(x and y) else x / y
0
>>> x, y = 2, ""
>>> 0 if not(x and y) else x / y
0
>>> x, y = 2, 1
>>> 0 if not(x and y) else x / y
2

但是,如果我明确检查变量的值为零,它会好一些,因为当两种类型不同或类型无法与零值进行比较时,它会引发错误,例如:

>>> x, y = 2, ""
>>> 0 if (x&y) == 0 else x / y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'int' and 'str'
>>> x,y = "",""
>>> 0 if (x&y) == 0 else x / y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'str' and 'str'
>>> x,y = [],[]
>>> 0 if (x&y) == 0 else x / y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'list' and 'list'

所以通常在定义检查数值的条件时,是不是像0 if (x|y) == 0 else x/y 代码更pythonic/更合适?

但它也有问题,因为它让 bool 类型溜走,导致一些非常令人不安的事情发生,例如 ZeroDivisionErrorValueError,例如:

>>> x,y = True, True
>>> 0 if (x&y) == 0 else x / y
1
>>> x,y = True, False
>>> 0 if (x&y) == 0 else x / y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> x,y = False, True
>>> 0 if (x&y) == 0 else x / y
0
>>> x,y = False, True
>>> 0 if (x&y) == 0 else math.log(x/y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error

此外,当变量类型是数字但有些不同时,这会导致问题:

>>> x, y = 1, 3.
>>> 0 if (x|y) == 0 else math.log(x/y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'int' and 'float'

还有 _or 运算符不能接受 float 的事实,这很奇怪:

>>> x, y = 1., 3.
>>> 0 if (x&y) == 0 and type(x) == type(y) == float else math.log(x/y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'float' and 'float'
>>> x, y = 1., 3.
>>> 0 if (x&y) == 0. and type(x) == type(y) == float else math.log(x/y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'float' and 'float'
>>> x, y = 1, 3
>>> 0 if (x&y) == 0 and type(x) == type(y) == float else math.log(x/y)
-1.0986122886681098

那么问题是:

  • 检查多个变量的零值的 pythonic 方法是什么?
  • 此外,重要的是不要喜欢 bool 类型滑动并引发错误而不是让它返回零,这怎么办?
  • TypeError: unsupported operand type(s) for |: 'float' and 'float' 应该如何解决以检查 (x|y) == 0 x 和 y 为 float 类型?

最佳答案

PEP 20考虑到“简单胜于复杂”,我会声称,如果您想检查一个值是否具有 bool 值以外的数字类型(请注意: bool 值 Python 中的数字类型,并且1/True 是有效的),最 pythonic 的方法就是明确地做到这一点,没有任何按位操作或依赖隐式检查。

import numbers

if not isinstance(y, numbers.Number) or type(y) is bool:
raise TypeError("y must be a number")
return x / y if y else 0

关于python - 检查数字零时的多条件声明 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34809884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com