2
我有這樣的python類A
。使用C++的參數創建Python構造函數,PyObject
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'
如何從import
的語句從C++實現對a.print_name("John")
? 任何幫助表示讚賞。
非常棒!奇蹟般有效。 – pseudo
真棒 - 感謝老兄! –