Вопрос

I want to create a list of ctypes.c_int32's

I have read and understand List of lists changes reflected across sublists unexpectedly.

No matter what I do, I keep creating a list of one instance of the c_int32. This works fine for int or floats or what not. Specifically I can not get ctypes to work.

f=[ctypes.c_int32]*10

but f[0] IS f[1], so...

f[0].value=1

changes f[1] as well. I know why it is doing it. How do I make it stop. That is, how do I make the list all unique instances of c_int32?

Это было полезно?

Решение

This is a classic Python mistake. To get around this you can create the list as:

f = [ctypes.c_int32() for i in range(10)]

This will do what you want. The other (cleaner) option is to create a ctypes array via:

f = (ctypes.c_int32 * 10)()
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top