diff --git a/special.html b/special.html index 76c521cc..92304aed 100644 --- a/special.html +++ b/special.html @@ -52,9 +52,7 @@ Python provides a number of special operatos for basic customization of a class:
-If we have a suitable C++ function that supports any of these features, we can
-export it like any other function, using its Python special name. For example,
-suppose that class Foo provides a string conversion function:
+If we have a suitable C++ function that supports any of these features, we can export it like any other function, using its Python special name. For example, suppose that class Foo provides a string conversion function:
std::string to_string(Foo const & f)
@@ -72,19 +70,13 @@ This function would be wrapped like this:
foo_class.def(&to_string, "__str__");
-Note that py_cpp also supports automatic wrapping in case of __str__
-and __cmp__. This is explained in the next section and
-the table of numeric operators.
+Note that py_cpp also supports automatic wrapping in case of __str__ and __cmp__. This is explained in the next section and the table of numeric operators.
Int (which might represent an infinite-precision integer)
-which support addition, so that we can write (in C++):
+There are two fundamental ways to define numeric operators within py_cpp: manual wrapping (as is done with general operators) and automatic wrapping. Lets start with the second possibility. Suppose, C++ defines a class Int (which might represent an infinite-precision integer) which support addition, so that we can write (in C++):
Int a, b, c;
@@ -106,19 +98,13 @@ Then we export the addition operator like this:
int_class.def(python::operators<python::op_add>());
-Since Int also supports subtraction, multiplication, adn division, we want to
-export those also. This can be done in a single command by 'or'ing the operator
-identifiers together (a complete list of these identifiers and the corresponding
-operators can be found in the table):
+Since Int also supports subtraction, multiplication, adn division, we want to export those also. This can be done in a single command by 'or'ing the operator identifiers together (a complete list of these identifiers and the corresponding operators can be found in the table):
int_class.def(python::operators<(python::op_sub | python::op_mul | python::op_div)>());
-Note that the or-expression must be enclosed in parentheses. This form of
-operator definition will wrap homogeneous operators, i.e. operators whose left
-and right operand have the same type. Now, suppose that our C++ library also
-supports addition of Ints and plain integers:
+Note that the or-expression must be enclosed in parentheses. This form of operator definition will wrap homogeneous operators, i.e. operators whose left and right operand have the same type. Now, suppose that our C++ library also supports addition of Ints and plain integers:
Int a, b;
@@ -128,19 +114,14 @@ supports addition of Ints and plain integers:
a = i + b;
-To wrap these heterogeneous operators (left and right hand side have different
-types), we need a possibility to specify a different type for one of the
-operands. This is done using the right_operand and
-left_operand templates:
+To wrap these heterogeneous operators (left and right hand side have different types), we need a possibility to specify a different type for one of the operands. This is done using the right_operand and left_operand templates:
int_class.def(python::operators<python::op_add>(), python::right_operand<int>());
int_class.def(python::operators<python::op_add>(), python::left_operand<int>());
-Py_cpp uses overloading to register several variants of the same operation (more
-on this in the context of coercion). Again, several
-operators can be exported at once:
+Py_cpp uses overloading to register several variants of the same operation (more on this in the context of coercion). Again, several operators can be exported at once:
int_class.def(python::operators<(python::op_sub | python::op_mul | python::op_div)>(),
@@ -150,31 +131,17 @@ operators can be exported at once:
-The type of the operand not mentioned is taken from the class object. In our
-example, the class object is int_class, and thus the other
-operand's type is `Int const &'. You can override this default
-by explicitly specifying a type in the operators template:
+The type of the operand not mentioned is taken from the class object. In our example, the class object is int_class, and thus the other operand's type is `Int const &'. You can override this default by explicitly specifying a type in the operators template:
int_class.def(python::operators<python::op_add, Int>(), python::right_operand<int>());
Here, `Int' would be used instead of `Int const &'.
-
-Note that automatic wrapping doesn't need any specific form of
-operator+() (or one of the other operators), but rather wraps the
-expression `left + right'. That is, this mechanism can be
-used for any definition of operator+(), such as a free function
-`Int operator+(Int, Int)' or a member function `Int
-Int::operator+(Int)'.
-
+Note that automatic wrapping doesn't need any specific form of operator+() (or one of the other operators), but rather wraps the expression `left + right'. That is, this mechanism can be used for any definition of operator+(), such as a free function `Int operator+(Int, Int)' or a member function `Int Int::operator+(Int)'.
-For the Python operators pow() and abs(), there is no
-corresponding C++ operator. Instead, automatic wrapping attempts to wrap C++
-functions of the same name. This only works if those functions are known in
-namespace python::detail. Thus it might be necessary to add a using
-declaration prior to wrapping:
+For the Python operators pow() and abs(), there is no corresponding C++ operator. Instead, automatic wrapping attempts to wrap C++ functions of the same name. This only works if those functions are known in namespace python::detail. Thus it might be necessary to add a using declaration prior to wrapping:
namespace python {
@@ -186,10 +153,7 @@ declaration prior to wrapping:
-In some cases, automatic wrapping of operators is not possible or not
-desirable. Suppose, for example, that the modulo operation for Ints is defined
-by a set of functions mod() (for automatic wrapping, we would need
-operator%()):
+In some cases, automatic wrapping of operators is not possible or not desirable. Suppose, for example, that the modulo operation for Ints is defined by a set of functions mod() (for automatic wrapping, we would need operator%()):
Int mod(Int const & left, Int const & right);
@@ -204,9 +168,7 @@ In order to create the Python operator "__mod__" from these functions, we have t
int_class.def((Int (*)(Int const &, int))&mod, "__mod__");
-The third form (with int as left operand) cannot be wrapped this
-way. We must first create a function rmod() with the operands
-reversed:
+The third form (with int as left operand) cannot be wrapped this way. We must first create a function rmod() with the operands reversed:
Int rmod(Int const & right, int left)
@@ -224,70 +186,41 @@ This function must be wrapped under the name "__rmod__":
A list of the possible operator names is also found in the table.
Special treatment is necessary to export the ternary pow operator.
-Automatic and manual wrapping can be mixed arbitrarily. Note that you cannot
-overload the same operator for a given extension class on both
-`int' and `float', because Python implicitly converts
-these types into each other. Thus, the overloaded variant found first (be it
-`int' or `float') will be used for either of the two
-types.
+Automatic and manual wrapping can be mixed arbitrarily. Note that you cannot overload the same operator for a given extension class on both `int' and `float', because Python implicitly converts these types into each other. Thus, the overloaded variant found first (be it `int' or `float') will be used for either of the two types.
Coercion
-Plain Python can only execute operators with identical types on the left and
-right hand side. If it encounters an expression where the types of the left and
-right operand differ, it tries to coerce these type to a common type before
-invoking the actual operator. Implementing good coercion functions can be
-difficult if many type combinations must be supported.
+Plain Python can only execute operators with identical types on the left and right hand side. If it encounters an expression where the types of the left and right operand differ, it tries to coerce these type to a common type before invoking the actual operator. Implementing good coercion functions can be difficult if many type combinations must be supported.
-In contrast, py_cpp provides overloading. By means of overloading, operator
-calling can be simplyfied drastically: you just register operators for all
-desired type combinations, and py_cpp automatically ensures that the correct
-function is called in each case. User defined coercion functions are not
-necessary. To enable operator overloading, py_cpp provides a standard
-coercion which is implicitly registered whenever automatic operator
-wrapping is used.
+In contrast, py_cpp provides overloading. By means of overloading, operator calling can be simplyfied drastically: you just register operators for all desired type combinations, and py_cpp automatically ensures that the correct function is called in each case. User defined coercion functions are not necessary. To enable operator overloading, py_cpp provides a standard coercion which is implicitly registered whenever automatic operator wrapping is used.
-If you wrap all operator functions manually, but still want to use operator
-overloading, you have to register the standard coercion function explicitly:
+If you wrap all operator functions manually, but still want to use operator overloading, you have to register the standard coercion function explicitly:
// this is not necessary if automatic operator wrapping is used
int_class.def_standard_coerce();
-In case you encounter a situation where you absolutely need a customized
-coercion, you can overload the "__coerce__" operator itself. The signature of a
-coercion function must look like this:
+In case you encounter a situation where you absolutely need a customized coercion, you can overload the "__coerce__" operator itself. The signature of a coercion function must look like this:
python::tuple custom_coerce(PyObject * left, PyObject * right);
-The resulting tuple must contain two elements which represent the
-values of left and right converted to the same
-type. Such a function is wrapped as usual:
+The resulting tuple must contain two elements which represent the values of left and right converted to the same type. Such a function is wrapped as usual:
some_class.def(&custom_coerce, "__coerce__");
-Note that the custom coercion function is only used if it is defined
-before any automatic operator wrapping on the given class or a call to
-`some_class.def_standard_coerce()'.
+Note that the custom coercion function is only used if it is defined before any automatic operator wrapping on the given class or a call to `some_class.def_standard_coerce()'.
The Ternary pow() Operator
-In addition to the usual binary pow()-operator (meaning
-x^y), Python also provides a ternary variant that implements
-(x^y) % z (presumably using a more efficient algorithm than
-concatenation of power and modulo operators). Automatic operator wrapping can
-only be used with the binary variant. Ternary pow() must always be
-wrapped manually. For a homgeneous ternary pow(), this is done as
-usual:
+In addition to the usual binary pow()-operator (meaning x^y), Python also provides a ternary variant that implements (x^y) % z (presumably using a more efficient algorithm than concatenation of power and modulo operators). Automatic operator wrapping can only be used with the binary variant. Ternary pow() must always be wrapped manually. For a homgeneous ternary pow(), this is done as usual:
Int power(Int const & first, Int const & second, Int const & module);
@@ -296,8 +229,7 @@ usual:
int_class.def((ternary_function1)&power, "__pow__");
-In case you want to support this function with non-uniform argument types,
-wrapping is a little more involved. Suppose, you have to wrap:
+In case you want to support this function with non-uniform argument types, wrapping is a little more involved. Suppose, you have to wrap:
Int power(Int const & first, int second, int module);
@@ -312,10 +244,7 @@ The first variant can be wrapped as usual:
int_class.def((ternary_function2)&power, "__pow__");
-In the second variant, however, Int appears only as second
-argument, and in the last one it is the third argument. Therefor we must first
-provide functions where the argumant order is changed so that Int
-appears in first place:
+In the second variant, however, Int appears only as second argument, and in the last one it is the third argument. Therefor we must first provide functions where the argumant order is changed so that Int appears in first place:
Int rpower(Int const & second, int first, int module)
@@ -592,31 +521,11 @@ Note that "__rrpow__" is an extension not present in plain Python.
Sequence and Mapping Operators
-Sequence and mapping operators let wrapped objects behave in accordance to
-Python's iteration and access protocols. These protocols differ considerably
-from the ones found in C++. For example, Python's typically iteration idiom
-looks like "for i in S:" , while in C++ one uses
- "for(iterator i = S.begin(); i != S.end(); ++i)". One could
-try to wrap C++ iterators in order to carry the C++ idiom into Python. However,
-this does not work very well because (1) it leads to non-uniform Python code
-(wrapped types must be used in a different way than Python built-in types) and
-(2) iterators are often implemented as plain C++ pointers which cannot be
-wrapped easily because py_cpp is designed to handle objects only.
-
+Sequence and mapping operators let wrapped objects behave in accordance to Python's iteration and access protocols. These protocols differ considerably from the ones found in C++. For example, Python's typically iteration idiom looks like "for i in S:" , while in C++ one uses "for(iterator i = S.begin(); i != S.end(); ++i)". One could try to wrap C++ iterators in order to carry the C++ idiom into Python. However, this does not work very well because (1) it leads to non-uniform Python code (wrapped types must be used in a different way than Python built-in types) and (2) iterators are often implemented as plain C++ pointers which cannot be wrapped easily because py_cpp is designed to handle objects only.
-Thus, it is a good idea to provide sequence and mapping operators for your
-wrapped containers. These operators have to be wrapped manually because there
-are no corresponding C++ operators that could be used for automatic
-wrapping. The Python documentation lists the relevant container
-operators. In particular, expose __getitem__, __setitem__ and remember to
-throw the PyExc_IndexError when the index is out-of-range in order
-to enable the "for i in S:" idiom.
-
+Thus, it is a good idea to provide sequence and mapping operators for your wrapped containers. These operators have to be wrapped manually because there are no corresponding C++ operators that could be used for automatic wrapping. The Python documentation lists the relevant container operators. In particular, expose __getitem__, __setitem__ and remember to throw the PyExc_IndexError when the index is out-of-range in order to enable the "for i in S:" idiom.
-Here is an example. Suppose you, we want to wrap a
-std::map<std::size_t,std::string>. This is done as follows as
-follows:
+Here is an example. Suppose you, we want to wrap a std::map<std::size_t,std::string>. This is done as follows as follows:
@@ -771,7 +680,7 @@ three
behavior. For example, when def_getter() is used, the
default functionality for setattr() and
delattr() remains in effect, operating on items in the extension
- obj's name-space (i.e., its __dict__). For that
+ instance's name-space (i.e., its __dict__). For that
reason, you'll usually want to stick with def_readonly and
def_read_write.
@@ -808,7 +717,7 @@ pair_int_long.def_read_write(&Pil::second, "second");
>>> x.second = 8
>>> x.second
8
->>> second(x) # Prove that we're not just changing the obj __dict__
+>>> second(x) # Prove that we're not just changing the instance __dict__
8