'the attribute of ctypes.py_object
I am trying to creating a python array and have problems with the following code
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()
In the initialization, it uses ctypes to create an array and I don't really understand the last two lines. I tried to change them into one line
self._elements = ctypes.py_object() * size
But it doesn't work and gives me the error
TypeError: unsupported operand type(s) for *: 'py_object' and 'int'
Could anyone explain it for me?
Solution 1:[1]
you want to mutiple with () simple remove parentheses and it will work
self._elements = ctypes.py_object * size
Solution 2:[2]
This will work self._elements = (size*ctypes.py_object)( )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Druta Ruslan |
| Solution 2 | Oluwatobiloba |
