Reputation: 4681
I've read in lots of places (including SO) that cython can convert structs to dictionaries, but I can't seem to find it in the documentation. Does this really happen? If so, under what circumstances does this occur?
I can't get it to do so using the following code:
# pxd:
cdef extern from "main.h":
ctypedef struct something_t:
int a
int b
# pyx:
cdef public void do_stuff(something_t *number):
number.a = 1 # works
number[0].a = 2 # works
number['a'] = 3 # doesn't work: Cannot assign type 'long' to 'something_t'
number[0]['a'] = 4 # doesn't work: Cannot assign type 'long' to 'something_t'
Upvotes: 1
Views: 911
Reputation: 2820
It happens when a variable of C struct type (cdef struct
or ctypedef struct
) is converted to a variable of Python object
type.
cdef something_t s = [1, 2] # declare and initialize C struct
cdef object sobj = s # sobj is assigned {'a': 1, 'b': 2} from s data
It's just an automatic conversion, nothing more. You can't use dict syntax with C structs or C struct syntax with Python dicts like in your example.
Upvotes: 2