如何查看变量的类型?(例如无符号32位)


当前回答

a = "cool"
type(a)

//result 'str'
<class 'str'>
or 
do 
`dir(a)` 
to see the list of inbuilt methods you can have on the variable.

其他回答

a = "cool"
type(a)

//result 'str'
<class 'str'>
or 
do 
`dir(a)` 
to see the list of inbuilt methods you can have on the variable.

没有32位、64位和16位,python很简单,你不必担心。看看如何检查类型:

integer = 1
print(type(integer))  # Result: <class 'int'>, and if it's a string then class will be str and so on.

# Checking the type
float_class = 1.3
print(isinstance(float_class, float))  # True

但如果真的需要,可以使用Ctypes库,它具有无符号整数等类型。

Ctypes类型文档

您可以这样使用:

from ctypes import *
uint = c_uint(1)  # Unsigned integer
print(uint)  # Output: c_uint(1)

# To actually get the value, you have to call .value
print(uint.value)

# Change value
uint.value = 2
print(uint.value)  # 2

这取决于你的意思。在Python2.x中,由于历史原因,有两种整数类型,int(限制为sys.maxint)和long(无限精度)。在Python代码中,这不会有什么区别,因为当数字太大时,解释器会自动转换为long。如果您想了解底层解释器中使用的实际数据类型,这取决于实现。(CPython位于Objects/intobject.c和Objects/longobject.c中)要了解系统类型,请查看使用结构模块的cdleary答案。

简单,适用于python 3.4及以上版本

print (type(variable_name))

Python 2.7及以上版本

print type(variable_name)

Python没有您所描述的类型。有两种类型用于表示整数值:int,它对应于C中平台的int类型;long,它是一个任意精度的整数(即,它根据需要增长,没有上限)。如果表达式产生的结果无法存储在int中,则int将被无声地转换为long。