C extension objects: possible to obtain __rmul__ functionality?
David Abrahams
dave at boost-consulting.com
Thu Mar 27 10:26:34 EST 2003
"Bernt Ribbum" <bernt at tordivel.no> writes:
> I wonder if this is at all possible:
>
> In a C extension module it is quite easy to create objects that can appear
> on the left side of e.g. a multiplication, as in
>
> CObject * PythonObject
>
> I find no way to make the object appear on the right side (PythonObject *
> CObject), except by overloading the __mul__ function in a Python class (of
> which PythonObject would be an instance in the example above). I would love
> to have this handled by a __rmul__ equivalent in my C class.
>
> Have anyone done this?
Boost.Python does it just fine by defining an __rmul__ method in the
extension type's __dict__. Of course, the extension types are derived
from object; not sure if that makes a difference.
Remember that in
PythonObject * CObject
if PythonObject is a new-style class, it must raise
NotImplementedError for this case in order to have CObject's __rmul__
method called. If it's an old-style class, you have to get involved
in __coerce__ ugliness.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Python-list
mailing list