From 1d5fb9798136c625127a818c3f0de496e2c0e996 Mon Sep 17 00:00:00 2001
From: Joel de Guzman
To illustrate, this Python code snippet:
- def f(x, f):
+ def f(x, y):
if (y == 'foo'):
x[3:7] = 'bar'
else:
- x.items += f(3, x)
+ x.items += y(3, x)
return x
def getfunc():
@@ -45,11 +45,11 @@ To illustrate, this Python code snippet:
Can be rewritten in C++ using Boost.Python facilities this way:
- object f(object x, object f) {
- if (f == "foo")
+ object f(object x, object y) {
+ if (y == "foo")
x.slice(3,7) = "bar";
else
- x.attr("items") += f(3, x);
+ x.attr("items") += y(3, x);
return x;
}
object getfunc() {
diff --git a/doc/tutorial/doc/building_hello_world.html b/doc/tutorial/doc/building_hello_world.html
index 8595ea56..297757ea 100644
--- a/doc/tutorial/doc/building_hello_world.html
+++ b/doc/tutorial/doc/building_hello_world.html
@@ -29,6 +29,17 @@ Now the first thing you'd want to do is to build the Hello World module and
try it for yourself in Python. In this section, we shall outline the steps
necessary to achieve that. We shall use the build tool that comes bundled
with every boost distribution: bjam.
+
+
+
+
Building without bjam
+
+Besides bjam, there are of course other ways to get your module built.
+What's written here should not be taken as "the one and only way".
+There are of course other build tools apart from bjam.
+
+
+
We shall skip over the details. Our objective will be to simply create the
hello world module and run it in Python. For a complete reference to
@@ -47,11 +58,13 @@ The hello.cpp file is our C++ hello world example. The Jamfile
minimalist bjam script that builds the DLLs for us.
Before anything else, you should have the bjam executable in your boost
-directory. Pre-built Boost.Jam executables are available for most
+directory or somewhere in your path such that bjam can be executed in
+the command line. Pre-built Boost.Jam executables are available for most
platforms. For example, a pre-built Microsoft Windows bjam executable can
be downloaded
here.
-The complete list of bjam pre-built executables can be found
+The complete list of bjam pre-built
+executables can be found
here.
Lets Jam!

diff --git a/doc/tutorial/doc/call_policies.html b/doc/tutorial/doc/call_policies.html
index 194097df..4f836700 100644
--- a/doc/tutorial/doc/call_policies.html
+++ b/doc/tutorial/doc/call_policies.html
@@ -131,7 +131,7 @@ first argument. In short: "return an internal reference X& own
Informs Boost.Python that the lifetime of the argument indicated by ward
(i.e. the 2nd argument: Z* z) is dependent on the lifetime of the
-argument indicated by custodian (i.e. the 1st argument: Z* z).
+argument indicated by custodian (i.e. the 1st argument: Y& y).
It is also important to note that we have defined two policies above. Two
or more policies can be composed by chaining. Here's the general syntax:
diff --git a/doc/tutorial/doc/class_operators_special_functions.html b/doc/tutorial/doc/class_operators_special_functions.html
index 74a952a2..ca7f6c0b 100644
--- a/doc/tutorial/doc/class_operators_special_functions.html
+++ b/doc/tutorial/doc/class_operators_special_functions.html
@@ -25,7 +25,7 @@
Python Operators
-C is well known for the abundance of oparators. C++ extends this to the
+C is well known for the abundance of operators. C++ extends this to the
extremes by allowing operator overloading. Boost.Python takes advantage of
this and makes it easy to wrap C++ operator-powered classes.
@@ -76,14 +76,22 @@ that correspond to these Python special functions. Example:
ostream& operator<<(ostream&,Rational);
class_<Rational>()
- .def(float_(self)) // __float__
- .def(pow(self)) // __pow__
- .def(abs(self)) // __abs__
- .def(str(self)) // __str__
+ .def(float_(self)) // __float__
+ .def(pow(self, other<Rational>)) // __pow__
+ .def(abs(self)) // __abs__
+ .def(str(self)) // __str__
;
Need we say more?
+
+ What is the business of operator<< .def(str(self))?
+Well, the method str requires the operator<< to do its work (i.e.
+operator<< is used by the method defined by def(str(self)). |
+
![]() |
diff --git a/doc/tutorial/doc/class_virtual_functions.html b/doc/tutorial/doc/class_virtual_functions.html
index 72c3fe3d..6116678a 100644
--- a/doc/tutorial/doc/class_virtual_functions.html
+++ b/doc/tutorial/doc/class_virtual_functions.html
@@ -137,14 +137,14 @@ Calling derived.f():
Will yield the expected result. Finally, calling calling the free function
call_f with derived as argument:
+ |
+ + Enums + | +
![]() |
+ ![]() |
+ ![]() |
+
+Boost.Python has a nifty facility to capture and wrap C++ enums. While +Python has no enum type, we'll often want to expose our C++ enums to +Python as an int while taking care of the proper conversions from +Python's dynamic typing to C++'s strong static typing (in C++, ints cannot +be implicitly converted to enums). Boost.Python's enum facility makes this +easy. To illustrate, given a C++ enum:
+
+ enum choice { red, blue };
+
++the construct:
+
+ enum_<choice>("choice")
+ .value("red", red)
+ .value("blue", blue)
+ ;
+
++can be used to expose it to Python. The new enum type is created in the +current scope(), which is usually the current module. The snippet above +creates a Python class derived from Python's int type which is +associated with the C++ type passed as its first parameter.
+
+ what is a scope?The scope is a class that has an +associated global Python object which controls the Python namespace in +which new extension classes and wrapped functions will be defined as +attributes. Details can be found +here |
+
+You can access those values in Python as
+
+ >>> my_module.choice.red
+ my_module.choice.red
+
++where my_module is the module where the enum is declared. You can create a +new scope around a class:
+
+ scope in_X(class_<X>("X")
+ .def( ... )
+ .def( ... )
+ );
+
+ // Expose X::nested as X.nested
+ enum_<X::nested>("nested")
+ .value("red", red)
+ .value("blue", blue)
+ ;
+
+![]() |
+ ![]() |
+ ![]() |
+
Copyright © 2002 David Abrahams
Copyright © 2002 Joel de Guzman
+Permission to copy, use, modify, sell and distribute this document
+ is granted provided this copyright notice appears in all copies. This document
+ is provided "as is" without express or implied warranty, and with
+ no claim as to its suitability for any purpose.
![]() |
![]() |
- ![]() |
+ ![]() |
@@ -66,7 +66,7 @@ facility in fact solves the mutable copying problem:




![]() |
- ![]() |
+ ![]() |
![]() |
The typical Python iteration protocol: for y in x... is as follows:
- iter iter = x.__iter__() #get iterator
+ iter = x.__iter__() #get iterator
try:
while 1:
y = iter.next() #get each item
@@ -87,7 +87,7 @@ Now, our C++ Wrapper:

- 
+ 

diff --git a/doc/tutorial/doc/object_interface.html b/doc/tutorial/doc/object_interface.html
index 2279a60c..d18a40bb 100644
--- a/doc/tutorial/doc/object_interface.html
+++ b/doc/tutorial/doc/object_interface.html
@@ -26,7 +26,7 @@
Python is dynamically typed, unlike C++ which is statically typed. Python -variables may hold an integers, a float, list, dict, tuple, str, long etc., +variables may hold an integer, a float, list, dict, tuple, str, long etc., among other things. In the viewpoint of Boost.Python and C++, these Pythonic variables are just instances of class object. We shall see in this chapter how to deal with Python objects.
diff --git a/doc/tutorial/doc/quickstart.txt b/doc/tutorial/doc/quickstart.txt index a8176ec1..8541612a 100644 --- a/doc/tutorial/doc/quickstart.txt +++ b/doc/tutorial/doc/quickstart.txt @@ -57,6 +57,13 @@ try it for yourself in Python. In this section, we shall outline the steps necessary to achieve that. We shall use the build tool that comes bundled with every boost distribution: [*bjam]. +[blurb __detail__ [*Building without bjam][br][br] + +Besides bjam, there are of course other ways to get your module built. +What's written here should not be taken as "the one and only way". +There are of course other build tools apart from [^bjam]. +] + We shall skip over the details. Our objective will be to simply create the hello world module and run it in Python. For a complete reference to building Boost.Python, check out: [@../../building.html building.html]. @@ -82,10 +89,12 @@ The [^hello.cpp] file is our C++ hello world example. The [^Jamfile] is a minimalist ['bjam] script that builds the DLLs for us. Before anything else, you should have the bjam executable in your boost -directory. Pre-built Boost.Jam executables are available for most +directory or somewhere in your path such that [^bjam] can be executed in +the command line. Pre-built Boost.Jam executables are available for most platforms. For example, a pre-built Microsoft Windows bjam executable can be downloaded [@http://boost.sourceforge.net/jam-executables/bin.ntx86/bjam.zip here]. -The complete list of bjam pre-built executables can be found [@../../../../../tools/build/index.html#Jam here]. +The complete list of bjam pre-built +executables can be found [@../../../../../tools/build/index.html#Jam here]. [h2 Lets Jam!] [$theme/jam.png] @@ -540,14 +549,14 @@ Calling [^derived.f()]: Will yield the expected result. Finally, calling calling the free function [^call_f] with [^derived] as argument: - >>> call_f(derived()) + >>> call_f(derived) 42 Will also yield the expected result. Here's what's happening: -# [^call_f(derived())] is called in Python +# [^call_f(derived)] is called in Python # This corresponds to [^def("call_f", call_f);]. Boost.Python dispatches this call. # [^int call_f(Base& b) { return b.f(); }] accepts the call. # The overridden virtual function [^f] of [^BaseWrap] is called. @@ -609,14 +618,14 @@ Calling [^call_f], passing in a [^base] object: Calling [^call_f], passing in a [^derived] object: - >>> call_f(derived()) + >>> call_f(derived) 42 [page:1 Class Operators/Special Functions] [h2 Python Operators] -C is well known for the abundance of oparators. C++ extends this to the +C is well known for the abundance of operators. C++ extends this to the extremes by allowing operator overloading. Boost.Python takes advantage of this and makes it easy to wrap C++ operator-powered classes. @@ -667,14 +676,18 @@ that correspond to these Python ['special functions]. Example: ostream& operator<<(ostream&,Rational); class_