-1

I have no experience with cython and just started learning it. I am trying to pass a module as an argument to a struct, but I don't know how? This is a sample code I tried in Jupyter-notebook:

%load_ext cython
%%cython
cdef class A:
    pass

cdef struct person:
    int num
    object info

cdef person someone
    p1.idd = 94113
    p1.info = A()

I would appreciate to help me with that.
Actually I am trying to replace the python dict lists in my code with self-designed structs because as I read here, it's not possible to use nogil with python dicts.
If anybody could find some way to overcome the problem in the way that code runs as fast as possible, that would be highly appreciated.
Thanks.

Masoud Masoumi Moghadam
  • 1,094
  • 3
  • 23
  • 45
  • 1
    I don't think structs can hold Python objects (such as modules) since it is not obvious how Cython should generate the reference counting code for them. In any case, you will need the gil to access the module. – DavidW Jul 28 '17 at 06:10

1 Answers1

1

The only things that can go into a C definition - what cdef describes - are C types.

Something like this would be a start, if you want to keep using structs:

ctypedef struct Info:
    char[256] address
    char[256] name
    <..>

ctypedef struct person:
    int num
    Info info

cdef class Person:
    cdef person my_struct

For simplicity, and since you aim to defined a class and use the object in Python anyway, can just make a cdef class and let Cython generate the structures for you.

cdef class Person:
    cdef char[256] name
    cdef char[256] address
    cdef int num

    <..>
danny
  • 5,140
  • 1
  • 19
  • 31