Home  >  Article  >  Backend Development  >  python类型强制转换long to int的代码

python类型强制转换long to int的代码

WBOY
WBOYOriginal
2016-06-16 08:46:492352browse

python2.4版本以后,如果int的值超出范围不会溢出,而是内部转换为long,在网上没有找到从long型强制转换成int的代码,这里所说的int取值范围是和java里一致,即用四个字节表示。
自己写了一个函数,勉强可以用,供大家参考。

复制代码 代码如下:

import sys
def LongToInt(value):
    assert isinstance(value, (int, long))
    return int(value & sys.maxint)

经过测试,在32位和64位上运算结果一致。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn