Python:整数精度和类型检查

Python:整数精度和类型检查,python,Python,我正在编写一个python(2.7.6)脚本,它从Web服务器提取JSON数据并将其发布到其他地方。我只想发布那些数字JSON值,例如没有子对象或字符串。数值很可能会超过int(在C意义上的大小)的大小。我当前的代码如下所示: for metric, currentValue in json.items() if type(currentValue) is int: oldValue = previous.get(metric) if oldValue i

我正在编写一个python(2.7.6)脚本,它从Web服务器提取JSON数据并将其发布到其他地方。我只想发布那些数字JSON值,例如没有子对象或字符串。数值很可能会超过
int
(在C意义上的大小)的大小。我当前的代码如下所示:

for metric, currentValue in json.items()
    if type(currentValue) is int:
        oldValue = previous.get(metric)
        if oldValue is None:
            oldValue = 0

        delta = currentValue - oldValue
        publish(metric, delta)
        previous[metric] = currentValue
我关心的是类型检查。如果Python决定
int
不再合适,而是使用
long
,这意味着某些度量将不会发布。如果超过
long
,该怎么办

我真正想要的是一种检查
currentValue
是否为数字的方法。
isdigit
,但这不适用于负片或浮点数。

您应该使用
isinstance
而不是
type

范例-

isinstance(currentValue, (int, long))

如果你也想考虑浮点,那么-< /P>

isinstance(currentValue, (int, long, float))

?
long
不能被超过(除非您的RAM超过,请参见示例),它是任意精度。请注意,
isdigit
仅适用于字符串;如果这就是您所拥有的,您将需要显式地将它们转换为数字。有关更具包容性的类型(
numbers.Integral
,例如,包括
int
long
bool
),请参阅模块。