简体   繁体   English

如何在cython中将模块作为struct的参数传递?

[英]How to pass a module as an argument for struct in cython?

I have no experience with cython and just started learning it.我没有使用 cython 的经验,刚刚开始学习它。 I am trying to pass a module as an argument to a struct , but I don't know how?我试图将模块作为参数传递给 struct ,但我不知道如何? This is a sample code I tried in Jupyter-notebook:这是我在 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.实际上,我试图用自己设计的结构替换我代码中的 python dict列表,因为正如我在此处阅读的那样,不可能将 nogil 与 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.谢谢。

The only things that can go into a C definition - what cdef describes - are C types.唯一可以进入 C 定义的东西cdef所描述的——是 C 类型。

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.为简单起见,并且由于您的目标是定义一个类并在 Python 中使用该对象,因此可以创建一个 cdef 类并让 Cython 为您生成结构。

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

    <..>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM