$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
From: Phil (phil.beadling_at_[hidden])
Date: 2007-06-11 17:17:37
Hi,
I'm a newbie having some problems instantiating a class written in C++ 
in Python using Boost.Python.
I can get a very simple example working when I expose a function outside 
of a class, that returns a string, but the below fails as shown, despite 
building.  The enum in the same library however works fine.
I've googled the error returned from Python and I get zero hits - has 
anyone get any idea what I'm doing wrong?
I'm using Python 2.5, GCC 4.0.1 and Boost 1.33 on Mac OS X 10.4.9.  I 
don't think it matters but I'm building the solution as a dynamic C++ 
bsd library in XCode and the changing the Mac OS X .dylib extention to 
.so as is required by Python.  This all works for simpler examples and 
the enum in this example.
Error: TypeError: __init__() should return None, not 'NoneType'
Thanks,
Phil.
 From header file example.h:
enum payOffType { CALL, PUT };
class Option
{
   public:
       //defaults to call
       Option( double strike, double expiry );
       double getPutCallMultiplier();
         private:
       double strike;
       double expiry;
       payOffType type;
};
 From cpp file example.cpp:
Option::Option( double strike, double expiry ) : strike( strike ), 
expiry( expiry ), type( CALL )
{
   cout << "Option Contructor called";
}
double Option::getPutCallMultiplier()
{
   return ( type == CALL ) ? 1 : -1;
}
BOOST_PYTHON_MODULE(libphil_boost)
{
   class_<Option>("Option", init<double, double>())
       .def("getPutCallMultiplier", &Option::getPutCallMultiplier)
   ;
   enum_<payOffType>("payOffType")
       .value("CALL", CALL)
       .value("PUT", PUT)
   ;
}
Using Python on the resulting .so:
$ python
Python 2.5.1 (r251:54863, Jun 10 2007, 17:57:56)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import libphil_boost
 >>> p = libphil_boost.Option( 23, 23 )
Option Contructor calledTraceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: __init__() should return None, not 'NoneType'
 >>>