繁体   English   中英

如何确定Python中的数字是32位还是64位整数?

[英]How to determine whether the number is 32-bit or 64-bit integer in Python?

我想区分Python中的32位和64位整数。 在C中,我们可以使用int_64int_32声明变量,这非常简单。 但是在Python中我们如何区分32位整数和64位整数?

没有必要。 解释器处理幕后的分配,根据需要有效地从一种类型推广到另一种类型,而无需您做任何明确的事情。

基本上,你没有。 没有理由。 如果要处理已知位大小的类型,请查看numpy数据类型。

如果你想数据放入指定格式,看结构模块。

其他答案中提到的struct模块就是你需要的东西。

一个清楚的例子。

import struct

struct.pack('qii', # Format string  here.
            100, # Your 64-bit integer
            50, # Your first 32-bit integer
            25) # Your second 32-bit integer

# This will return the following:
'd\x00\x00\x00\x00\x00\x00\x002\x00\x00\x00\x19\x00\x00\x00'

格式化字符串的文档

来自ipython解释器会话的以下片段指示了一种测试整数类型的方法。 注意,在我的系统上, int是64位数据类型, long是多字类型。

In [190]: isinstance(1,int)
Out[190]: True    
In [191]: isinstance(1,long)
Out[191]: False    
In [192]: isinstance(1L,long)
Out[192]: True

另请参阅有关sys.getsizeof 的答案 此函数并不完全相关,因为包含了一些额外的开销字节。 例如:

In [194]: import sys    
In [195]: sys.getsizeof(1)
Out[195]: 24
In [196]: sys.getsizeof(1L)
Out[196]: 28

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM