[C++-sig] Getting a Python type object for a c++ type in wrapper code...

Nick Rasmussen nick at ilm.com
Tue Oct 4 22:13:09 CEST 2005


object(handle<>(borrowed(&PyFloat_Type)))?

not sure if there's a more boost-ish way of doing it...

-nick

On Tue, 04 Oct 2005, Alex Mohr wrote:

> Of course!  That makes perfect sense.  Thanks Nick and Mike.
> 
> The only remaining question is if there's an easy way to do this with 
> types like python's 'float' type instead of my own.  Would I have to 
> create something like boost::python::long_ for floats, or is there a 
> better way?
> 
> Thanks again,
> 
> Alex
> 
> 
> Nick Rasmussen wrote:
> > Perhaps something like:
> > 
> > #include <Python.h>
> > #include <boost/python.hpp>
> > 
> > using namespace boost::python;
> > 
> > struct Foo {
> > 
> > };
> > 
> > struct Bar {
> >   static object fooType;
> > };
> > 
> > object Bar::fooType;
> > 
> > BOOST_PYTHON_MODULE(test)
> > {
> >   class_<Foo> fooType("Foo");
> >   Bar::fooType = fooType;
> >   class_<Bar> barType("Bar");
> >   barType
> >     .def_readonly("fooType",&Bar::fooType);
> > }
> > 
> > -nick
> > 
> > 
> > On Tue, 04 Oct 2005, Alex Mohr wrote:
> > 
> > 
> >>Hi folks,
> >>
> >>I'm a relative boost.python newbie but I've been doing a ton with it and 
> >>I'm having a blast.  Thanks for such an awesome package.
> >>
> >>Here's my first newbie question.
> >>
> >>Suppose I have two C++ classes that I'm wrapping -- Foo and Bar.  In the 
> >>wrapper for Bar, I want there to be an attribute which holds the type 
> >>Foo.  That is, from python I wan to be able to say:
> >>
> >> >>> Module.Bar.fooType == Module.Foo
> >>True
> >>
> >>Something like def_readonly("fooType", <var-which-holds-type-for-Foo>) 
> >>would be ideal.  I'd also like to be able to do this for python types, 
> >>like python's float.  Is there a simple way to do this in my 
> >>Boost.Python code?
> >>
> >>Thanks,
> >>
> >>Alex
> >>
> >>_______________________________________________
> >>C++-sig mailing list
> >>C++-sig at python.org
> >>http://mail.python.org/mailman/listinfo/c++-sig
> > 
> > _______________________________________________
> > C++-sig mailing list
> > C++-sig at python.org
> > http://mail.python.org/mailman/listinfo/c++-sig
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig



More information about the Cplusplus-sig mailing list