Add in an example/test for Python __pow__

- A test for the ternaryfunc builtin slot
- Example of how to wrap C++ class for Python's pow
This commit is contained in:
William S Fulton 2018-09-16 17:24:37 +01:00
commit bf85b6f7a9
2 changed files with 34 additions and 0 deletions

View file

@ -226,3 +226,22 @@ void Dealloc2Destroyer(PyObject *v) {
};
%}
// Test 7 mapping to Python's pow
%pybinoperator(__pow__, ANumber::power, ternaryfunc, nb_power);
%inline %{
class ANumber {
int num;
public:
ANumber(int d = 0) : num(d) {}
ANumber __pow__(const ANumber &other, const ANumber *x) const {
int val = (int)pow(num, other.num);
val = x ? val % x->num : val;
return ANumber(val);
}
int Value() const {
return num;
}
};
%}