[C++-sig] A couple of simple BP V2 questions
Scott A. Smith
ssmith at magnet.fsu.edu
Tue Nov 12 22:21:24 CET 2002
Thanks for the response.
> > 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.
> > I don't recall ever needing the module name for any exposed classes
> > from V1.
> It was no different in V1.
Well, you forced me to search for a way to explain how you could possibly
I've never before even tried using the module name method and was fairly
using my exported classes with V1. What I found out was that I didn't know
>>> import hello and >>> from hello import *
Now I do, probably about doubling my Python abilities. Since the day
I first tried Python (not so long ago, as this was the day I first saw
Boost.Python also), I have always used "from PyModule import *" and never
any different. Nor can I find mention of it in my O'Reilly Python book. But
I did find out about it from a Google search and then
http://www.python.org/doc/current/ref/import.html, so that mystery is
I take it good Python programmers largely frown on using "from PyModule
As for where the tutorial seems to not have any module name, on
>>> x = Var('pi')
should be something like x = hello.Var('pi') if it is in the hello module,
I am again mistaken.
> > How do I exposes a simple constant, not
> > in any class but globally defined.
> scope().attr("PLANCK") = 6.6260755e-34;
But this is a re-definition isn't it? I don't mind doing that, but then
if I change my C++ code (maybe to add more significant digits or the
foundations of physics change) I'll have to change the Python export too.
I can live with that though.
> > 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.
I guess that is still OK. But then would the statement
>>> X = PyModule.PLANCK
>>> Y = 1.e-27
PyModule.PLANCK = 1.e-27
actually alter what the value of PLANCK is within the Python interpreter?
Thanks Dave, this should keep me going for a while.
More information about the Cplusplus-sig