Embedding Python and built-in types

Martin v. Löwis martin at v.loewis.de
Fri Nov 29 15:57:06 EST 2002


"Mickael Putters" <mputters at dreamingprophet.com> writes:

> Is it possible to embed it so that you have control over the simple
> types operators ?

You can overload those operators for user-defined types. For builtin
types, the builtin implementation is used.

> Basically, that you can for example handle yourself the operator =
> for integers (actually, just being notified of it happening is
> enough, since it's to compile some part of the language to
> bytecode).

I assume you are talking about the equality operator here, which is
spelled ==.

When embedding Python, please understand that you have the full
source; Python is free software. So if you want to modify the
implementation of integer comparison: feel free to. It is in
Objects/intobject.c:int_compare.

If you don't want to modify the source, you have to understand that
Python does not implement an equality operator for the int type
(although it does so for other types). For types not implementing ==,
Python uses their compare operation instead, which gives -1/0/1; if
the result is 0, the values are equal.

If you can accept to modify that instead of == operator, then you can
modify, at run time, PyInt_Type.tp_compare. All uses of the compare
function should use this function pointer, so your code should be
called instead of the standard compare implementation.

HTH,
Martin




More information about the Python-list mailing list