我有一个函数foo
调用math.isclose
:
import math
def foo(..., rtol=None, atol=None):
...
if math.isclose(x, y, rel_tol=rtol, abs_tol=atol):
...
...
如果我没有通过rtol
和atol
至foo
,以上内容将在math.isclose
分失败:
TypeError: must be real number, not NoneType
我不想在代码中使用系统默认参数值(什么
以下是我到目前为止的 idea :
import math
def foo(..., rtol=None, atol=None):
...
tols = {}
if rtol is not None:
tols["rel_tol"] = rtol
if atol is not None:
tols["abs_tol"] = atol
if math.isclose(x, y, **tols):
...
...
这看起来又长又傻,每次调用
那么,告诉math.isclose
使用默认值的最佳方式是什么
还有几个相关的问题.请注意,我不想知道math.isclose
的实际默认参数——我只想让它告诉它使用默认值,不管它们是什么.