Correct notes on customising Python builtin comparison operators

Also add example for python:compare feature
This commit is contained in:
William S Fulton 2016-08-22 07:22:40 +01:00
commit 4f777b181c
3 changed files with 52 additions and 12 deletions

View file

@ -18,6 +18,13 @@
where <slot> is the name of a field in a PyTypeObject, PyNumberMethods,
PyMappingMethods, PySequenceMethods, or PyBufferProcs. For example:
%feature("python:tp_hash") MyClass "myHashFunc";
class MyClass {
public:
...
};
%{
// Note: Py_hash_t was introduced in Python 3.2
static Py_hash_t myHashFunc(PyObject *pyobj) {
@ -27,13 +34,6 @@
}
%}
%feature("python:tp_hash") MyClass "myHashFunc";
class MyClass {
public:
...
};
NOTE: It is the responsibility of the programmer (that's you) to ensure
that a statically defined slot function has the correct signature.
@ -62,20 +62,21 @@
operator overloads for comparison (operator==, operator<, etc.), they
will be called from the generated rich compare function. If you
want to explicitly choose a method to handle a certain comparison
operation, you may use %feature("python:slot") like this:
operation, you may use a different feature, %feature("python:compare")
like this:
%feature("python:compare", "Py_LT") MyClass::lessThan;
class MyClass {
public:
bool lessThan (const MyClass& x) const;
bool lessThan(const MyClass& other) const;
...
};
%feature("python:slot", "Py_LT") MyClass::lessThan;
... where "Py_LT" is one of the rich comparison opcodes defined in the
python header file object.h.
If there's no method defined to handle a particular comparsion operation,
If there's no method defined to handle a particular comparison operation,
the default behavior is to compare pointer values of the wrapped
C++ objects.