开发者

implementing callback between Python and C

开发者 https://www.devze.com 2023-01-21 17:03 出处:网络
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 strin

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

精彩评论

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

关注公众号