python - ctypes.py_object 的属性
问题描述
我正在尝试创建一个 python 数组,但下面的代码有问题
def __init__(self, size):
assert size>0, "Array size must be > 0"
self._size = size
# Create the array structure using the ctypes module.
arraytype = ctypes.py_object * size
self._elements = arraytype()
在初始化中,它使用 ctypes 创建一个数组,我不太了解最后两行。我试图将它们更改为一行
self._elements = ctypes.py_object() * size
但它不起作用并给我错误
TypeError: unsupported operand type(s) for *: 'py_object' and 'int'
谁能为我解释一下?
解决方案
ctypes.py_object
是一种类型ctypes.py_object * size
是一种类型ctypes.py_object()
是一个类型的实例
您要做的是先获取ctypes.py_object * size
类型,然后实例化它:
self._elements = (ctypes.py_object * size)()
尽管您可能想要使用 Python 列表,但我不确定您是否需要 ctypes 数组。例子:
self._elements = [None] * size
推荐阅读
- powerbi - 忽略 PowerBI 发送到 SSAS 的过滤器之一
- java - 如何在控制台中的启动时设置数据库连接参数 jar spring-boot app
- docker - 如何正确组合这个 docker-compose.yml?
- python - 如何将 youtube-dl python 设置为始终选择 1080p
- python - Jupyter Notebook)是否可以通过添加对应于数据集中公共列的行来创建新数据集?
- c++ - 泄密者:发现错误!内存没有被释放
- php - 爆炸图像base64时Laravel未定义偏移量1
- mongodb - 使用 MongoDB Atlas 的 REST API,在数据库中发布条目时 findOne() 出现问题
- sql - 使用 Hive 将列转置为同一表中的行
- javascript - 元素没有隐藏在 javascript 中