无法访问C++;使用boostpython公开的类方法 我是Python的新手,现在尝试用Booj.python,将一些C++ API暴露给Python。

无法访问C++;使用boostpython公开的类方法 我是Python的新手,现在尝试用Booj.python,将一些C++ API暴露给Python。,c++,python,boost,C++,Python,Boost,我从一个简单的类开始,这个类接近我想要实现的目标: class BaseImpl { public: BaseImpl() {} virtual int f() { return 10; } }; class BaseImplWrap : public BaseImpl, public wrapper<BaseImpl> { public: int f() { return this->get_

我从一个简单的类开始,这个类接近我想要实现的目标:

class BaseImpl
{
public:
    BaseImpl() {}

    virtual int f()
    {
        return 10;
    }
};

class BaseImplWrap : public BaseImpl, public wrapper<BaseImpl>
{
public:
    int f()
    {
        return this->get_override("f")();
    }
};
classbaseimpl
{
公众:
BaseImpl(){}
虚拟整数f()
{
返回10;
}
};
类BaseImplWrap:publicBaseImpl,publicWrapper
{
公众:
int f()
{
返回此->获取覆盖(“f”)();
}
};
并披露如下:

BOOST_PYTHON_MODULE(test)
{
    class_<BaseImplWrap, boost::noncopyable>("BaseImpl", init<>())
        .def("f", &BaseImpl::f)
        ;
}
BOOST\u PYTHON\u模块(测试)
{
类(“BaseImpl”,init())
.def(“f”和&BaseImpl::f)
;
}
当我试图访问PythonWin上的公开类方法时,出现以下错误:

PythonWin 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
>>> import test
>>> a=test.BaseImpl()
>>> a.fFailed to format the args
Traceback (most recent call last):
  File "C:\Program Files\Python\Lib\site-packages\pythonwin\pywin\idle\CallTips.py", line 130, in get_arg_text
    argText = inspect.formatargspec(*arg_getter(fob))
  File "C:\Program Files\Python\lib\inspect.py", line 813, in getargspec
    raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <Boost.Python.function object at 0x000000000533ADF0> is not a Python function
namespace  {
    class hello
    {
        public:
            hello(const std::string& country) { this->country = country; }
            std::string greet() const { return "Hello from " + country; }

        private:
            std::string country;
    };
}

BOOST_PYTHON_MODULE(test)
{
    class_<hello>("hello", init<std::string>())
        .def("greet", &hello::greet)  // Add a regular member function.
        ;
}

>>> import test
>>> a=test.hello('world')
>>> a.greetFailed to format the args
Traceback (most recent call last):
  File "C:\Program Files\Python\Lib\site-packages\pythonwin\pywin\idle\CallTips.py", line 130, in get_arg_text
    argText = inspect.formatargspec(*arg_getter(fob))
  File "C:\Program Files\Python\lib\inspect.py", line 813, in getargspec
    raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <Boost.Python.function object at 0x0000000002FEACA0> is not a Python function
win32上的Pythonwin2.7(r27:825252010年7月4日07:43:08)[MSC v.1500 64位(AMD64)]。 部分版权1994-2008 Mark Hammond-有关更多版权信息,请参阅“帮助/关于蟒蛇”。 >>>导入测试 >>>a=test.BaseImpl() >>>a.f用于格式化参数 回溯(最近一次呼叫最后一次): 文件“C:\Program Files\Python\Lib\site packages\pythonwin\pywin\idle\CallTips.py”,第130行,在get\u arg\u文本中 argText=inspect.formatargspec(*arg_getter(fob)) getargspec中第813行的文件“C:\Program Files\Python\lib\inspect.py” raise TypeError(“{!r}不是Python函数”。format(func)) TypeError:不是Python函数 然后,我尝试了教程中更简单的方法,但仍然无法使其工作,出现以下错误:

PythonWin 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
>>> import test
>>> a=test.BaseImpl()
>>> a.fFailed to format the args
Traceback (most recent call last):
  File "C:\Program Files\Python\Lib\site-packages\pythonwin\pywin\idle\CallTips.py", line 130, in get_arg_text
    argText = inspect.formatargspec(*arg_getter(fob))
  File "C:\Program Files\Python\lib\inspect.py", line 813, in getargspec
    raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <Boost.Python.function object at 0x000000000533ADF0> is not a Python function
namespace  {
    class hello
    {
        public:
            hello(const std::string& country) { this->country = country; }
            std::string greet() const { return "Hello from " + country; }

        private:
            std::string country;
    };
}

BOOST_PYTHON_MODULE(test)
{
    class_<hello>("hello", init<std::string>())
        .def("greet", &hello::greet)  // Add a regular member function.
        ;
}

>>> import test
>>> a=test.hello('world')
>>> a.greetFailed to format the args
Traceback (most recent call last):
  File "C:\Program Files\Python\Lib\site-packages\pythonwin\pywin\idle\CallTips.py", line 130, in get_arg_text
    argText = inspect.formatargspec(*arg_getter(fob))
  File "C:\Program Files\Python\lib\inspect.py", line 813, in getargspec
    raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <Boost.Python.function object at 0x0000000002FEACA0> is not a Python function
名称空间{
同学们好
{
公众:
你好(const std::string&country){this->country=country;}
std::string greet()常量{return“Hello from”+country;}
私人:
std::字符串国家;
};
}
BOOST_PYTHON_模块(测试)
{
类(“hello”,init())
.def(“greet”、&hello::greet)//添加常规成员函数。
;
}
>>>导入测试
>>>a=test.hello(‘世界’)
>>>a.Greet无法格式化参数
回溯(最近一次呼叫最后一次):
文件“C:\Program Files\Python\Lib\site packages\pythonwin\pywin\idle\CallTips.py”,第130行,在get\u arg\u文本中
argText=inspect.formatargspec(*arg_getter(fob))
getargspec中第813行的文件“C:\Program Files\Python\lib\inspect.py”
raise TypeError(“{!r}不是Python函数”。format(func))
TypeError:不是Python函数
知道为什么公开的类方法不能被识别为python函数吗?这与我的系统配置有关吗

我在一台windows7x64机器上运行,使用python2.7boost 1.54.0MSVC 2012 x64


提前感谢。

hello的构造函数将
std::string
作为参数,而python代码中没有提供该参数。试试这个:
a=test.hello('USA')
Hi Gareth,谢谢你指出错误。我试过了,但在访问a.greet()时遇到了相同的错误:TypeError:。。。不是Python函数。知道吗?在我看来像是蟒蛇的错误。你会尝试使用命令行界面吗?