如何查看变量的类型?(例如无符号32位)
当前回答
您应该使用type()函数。像这样:
my_variable = 5
print(type(my_variable)) # Would print out <class 'int'>
此函数将查看任何变量的类型,无论是列表还是类。查看此网站了解更多信息:https://www.w3schools.com/python/ref_func_type.asp
其他回答
你的意思是用Python还是用ctypes?
在第一种情况下,您根本不可能——因为Python没有带符号/无符号的16/32位整数。
在第二种情况下,可以使用type():
>>> import ctypes
>>> a = ctypes.c_uint() # unsigned int
>>> type(a)
<class 'ctypes.c_ulong'>
有关ctypes及其类型的更多参考信息,请参阅官方文档。
对于python2.x,请使用
print type(variable_name)
对于python3.x,请使用
print(type(variable_name))
没有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
我在刚接触Python时(现在仍然如此)看到了这个:
x = …
print(type(x))```
这可能无关紧要。但您可以使用isinstance(object,type)检查对象的类型,如这里所述。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录