I have a C struct and a constructor
typedef struct {
uint32_t index;
int32_t line;
int16_t column;
} Position;
Position positionCreate() {
Position position;
position.index = 0;
position.line = -1;
position.column = -1;
return position;
}
That has a Python class equivalent
from ctypes import *
library = cdll.LoadLibrary('./libposition.so')
class Position(Structure):
_fields_ = [
('index', c_uint32),
('line', c_int32),
('column', c_int16),
]
When I initialize the class "manually":
constructor = library.positionCreate
constructor.restype = Position
position = constructor()
print(position.index, position.line, position.column)
I get the desired output:
0, -1, -1
But I want to use this constructor:
class Position(Structure):
_fields_ = [
('index', c_uint32),
('line', c_int32),
('column', c_int16),
]
def __init__(self):
# Or words to this effect
self = library.positionCreate()
And when I try this:
position = Position()
print(position.index, position.line, position.column)
I no longer get the right output:
0, 0, 0
I think my syntax of self = positionCreate() is wrong, but I don't know what would be right.