当我在Boost.Python中使用类时,我收到错误TypeError: __init__() should return None, not 'NoneType'
(我可以运行简单的“Hello,world”)。
环境
pyenv-virtualenv
$ source activate py36
$pyenv global py36
$brew install boost
$brew install boost-python --with-python3
代码
这是我尝试过的简单代码(test.cpp
)。
#include <boost/python.hpp>
using namespace boost::python;
int main(){
}
class accumulator {
public:
int operator()(int v) {
v_ += v;
return v_;
}
int value() const {
return v_;
}
private:
int v_;
};
BOOST_PYTHON_MODULE(test)
{
class_<accumulator>("accumulator")
.def("__call__",
&accumulator::operator())
.add_property("value", &accumulator::value)
;
}
生成文件
CC = clang++ -std=c++11 -stdlib=libc++
PYTHON_ROOT = /usr/local/var/pyenv/versions/3.6.0
PYTHON = -lpython3.6m -lboost_python3
# location of the Boost Python include files and library
BOOST_INC = /usr/local/Cellar/boost-python/1.64.0/include
BOOST_LIB = /usr/local/Cellar/boost-python/1.64.0/lib
# compile mesh classes
TARGET = test
$(TARGET).so: $(TARGET).o
$(CC) $(TARGET).o $(PYTHON) -I$(PYTHON_ROOT)/include/python3.6m -L$(BOOST_LIB) -L$(PYTHON_ROOT)/lib -o $(TARGET).so
$(TARGET).o: $(TARGET).cpp
$(CC) -I$(PYTHON_ROOT)/include/python3.6m -I$(BOOST_INC) -c $(TARGET).cpp
编译
$ make
clang++ -std=c++11 -stdlib=libc++ -I/usr/local/var/pyenv/versions/3.6.0/include/python3.6m -I/usr/local/Cellar/boost-python/1.64.0/include -c test.cpp
clang++ -std=c++11 -stdlib=libc++ test.o -lpython3.6m -lboost_python3 -I/usr/local/var/pyenv/versions/3.6.0/include/python3.6m -L/usr/local/Cellar/boost-python/1.64.0/lib -L/usr/local/var/pyenv/versions/3.6.0/lib -o test.so
$ python
Python 3.6.0 (default, Dec 28 2016, 23:31:45)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> a = test.accumulator()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() should return None, not 'NoneType'
我在Japanese中做了同样的问题。如果我在其中任何一个中得到它,我会翻译解决方案。