开发者

implementing callback between Python and C

I have wrapped some C code using SWIG to use it as a python library.

Within this framework, some python code I have written calls a C function, which returns a string. However, for creating the string, the C function requires a ranking, the generation of which I have implemented in Python. How would I go about implementing this using callbacks?

I see this as the following multistep process.

1) Python instantiates a C object: import test_Objects #test_Objects is the C file t开发者_JAVA技巧hat has been wrapped C = test_objects.my_class()

2) Call the relevant method on the my_class object, which return a string: txt_1 = "string1" txt_2 - "string2" result = C.sorted_string(txt_1, txt_2)

2.1) I want sorted_string to call the following python function, which returns a sorted list.

   def sorted_string([my_list]):
       .....
       .....
       return your_list

2.2) Sorted_string would make use of the list to generate the result.

How would I implement step 2.1?


The PyObject_Call*() functions can be used to call a Python function or method from C.


It is not specific to your question, but here is an example that I asked: swig-passing-argument-to-python-callback-function

Hope this is helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜