我有一个A
像这样的python类。
class A:
def __init__(self, name):
self.name = name
def print_lastname(self, lastname):
print(lastname)
我必须像这样调用这段代码。
import B
a = B.A("hello")
a.print_lastname("John")
目前,我需要A
从我的 C++ 代码中使用这个类。我已经走到这一步了。
Py_Initialize();
string hello = "hello";
PyObject *module, *attr, *arg;
module = PyObject_ImportModule("B"); // import B
attr = PyObject_GetAttrString(module, "A"); // get A from B
arg = PyString_FromString(hello.c_str());
instance = PyInstance_New(attr, arg, NULL); // trying to get instance of A with parameter "hello"
Py_Finalize();
但我收到错误
异常类型错误:来自“/usr/lib64/python2.7/threading.pyc”的模块“线程”中的“参数列表必须是元组”
如何实现从import
语句到a.print_name("John")
C++?任何帮助表示赞赏。