[C++-sig] A couple of simple BP V2 questions
David Abrahams
dave at boost-consulting.com
Tue Nov 12 20:42:01 CET 2002
"Scott A. Smith" <ssmith at magnet.fsu.edu> writes:
> I am biting the bullet and switching my project over to use V2 finally.
> Many thanks to everyone working on the BP, the new documentation looks
> terrific. I am hoping someone can take the time to answer a couple of
> very simple questions to get me started.
>
> 1.) In the Hello tutorial, I see that one uses the Python Module name
> when creating instances of a class object.
>
> >>> import hello
> >>> planet = hello.World()
>
> But in other places, it seems like the module name is not
> required.
Where?
> I don't recall ever needing the module name for any exposed classes
> from V1.
It was no different in V1.
> I understand that once planet (above) is made one does not need
> hello. to access its members, but is it always true that one
> needs it when declaring class objects? What about global
> functions? Is there a way to set things so they do not need
> this?
>>> from hello import *
> 2.) The tutorial begins dealing with classes, and looks so nice that I
> doubt I'll have many problems switching to use V2. But on simpler things
> I am at a loss. At the start of my project, before I even get to dealing
> with my C++ classes, I have some global variables and functions
> declared.
> For example,
>
> extern const double PLANCK; (in .hpp)
> const double PLANCK = 6.6260755e-34; (in .cpp)
>
> which in BP V1 I exposed using something like
>
> PyModName.add(boost::python::make_ref(PLANCK), "PLANCK");
>
> Now make_ref & PyModName.add are gone, so I took a guess & tried
>
> def("PLANCK", PLANCK, reference_existing_object());
>
> which compiles but doesn't link. How do I exposes a simple constant, not
> in any class but globally defined.
scope().attr("PLANCK") = 6.6260755e-34;
> Can I do it "read-only" as suggested
> in the tutorial when explaining class Var where it uses
> .def_readonly("name", &Var::name)?
Nope. This is a Python feature/limitation. There's no way to add
properties to modules, and they don't have overridable
__getattr__/__setattr__ functions.
--
David Abrahams
dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution
More information about the Cplusplus-sig
mailing list