python - 比较简单的 Python ctypes 数据类型

标签 python ctypes

>>> ctypes.c_ushort(37099) == ctypes.c_ushort(37099)
False

这些似乎不相等,因为 PyCSimpleType 对象没有比较运算符。

省略这些有什么原因吗?看来必须使用:

>>> ctypes.c_ushort(37099).value == ctypes.c_ushort(37099).value
True

最佳答案

根据 ctypes._SimpleCData 的文档及其父类(super class) ctypes._CData , “所有 ctypes 类型实例都包含一个存储 C 兼容数据的内存块。”

据推测,ctypes.c_ushort(37099) 的每次调用都对应一个新的内存块,从而使它们之间的相等比较为假。该文档还指出 value 属性包含对象的实际值。

>>> import ctypes
>>> val1 = ctypes.c_ushort(37099)
>>> val2 = ctypes.c_ushort(37099)
>>> ctypes.addressof(val1)
2193186894992
>>> ctypes.addressof(val2)
2193186895376

关于python - 比较简单的 Python ctypes 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386315/

相关文章:

python - 如何将数组传递给使用 python 用 c 编写的共享库 (.dll)

python - 如何避免 pandas 在保存的 csv 中创建索引

python - cv2.resize() 错误 : saves the same picture with different names

Python 使用 optparse 指定日期格式

python - ctypes - 没有形状的numpy数组?

python - 如何在 Python 中打印 c_ubyte_Array 对象

python - 处理 Ctype 中的默认参数

python - 使用 Tensorflow 概率进行分位数回归

python - 使用 Python ctypes 的 C 函数名称相关段错误

swig - protobuf 是 ctypes/SWIG/cython 等现有 python 包装器的替代品吗?