[C++-sig] A couple of simple BP V2 questions

David Abrahams dave at boost-consulting.com
Tue Nov 12 23:15:53 CET 2002

"Scott A. Smith" <ssmith at magnet.fsu.edu> writes:

> Hi Dave,
> 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.
>> Where?
>> >     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 state that!  I've never
> before even tried using the module name method and was fairly successful using my exported classes with
> V1. What I found out was that I didn't know the difference between
> >>> 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 knew 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 solved.  I take it good Python programmers largely frown on using
> "from PyModule import *"?

Yes, and rightly so.

> As for where the tutorial seems to not have any module name, on
> http://www.boost.org/libs/python/doc/tutorial/doc/class_data_members.html
> the line
>>>> x = Var('pi')
> should be something like x = hello.Var('pi') if it is in the hello
> module, unless I am again mistaken.

Yes, I guess you're right. We start leaving out a bunch of detail on
that page. Joel, would you consider what we can do to clarify that?

>> >    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?

Huh? What do you mean by "re-definition"?

> 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.

Oh, well of course you can write:

    scope().attr("PLANCK") = PLANCK;

>> >     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

???syntax error---------^

> or even
> PyModule.PLANCK = 1.e-27
> actually alter what the value of PLANCK is within the Python interpreter?

Sure. It's just a binding in the module.

                       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