【发布时间】:2016-10-19 22:13:38
【问题描述】:
我有一个这样的python类A。
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")
目前,我需要在我的 C++ 代码中使用这个 A 类。我已经走到这一步了。
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();
但是我遇到了错误
Exception TypeError: 'argument list must be tuple' in module 'threading' from '/usr/lib64/python2.7/threading.pyc'
如何从 C++ 实现从 import 语句到 a.print_name("John")?
任何帮助表示赞赏。
【问题讨论】:
标签: python c++ tuples python-embedding pyobject