Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


Why doesn't my *= operator work?

Q: I have exported my class to python, with many overloaded operators. it works fine for me except the *= operator. It always tells me "can't multiply sequence with non int type". If I use p1.__imul__(p2) instead of p1 *= p2, it successfully executes my code. What's wrong with me?

A: There's nothing wrong with you. This is a bug in Python 2.2. You can see the same effect in Pure Python (you can learn a lot about what's happening in Boost.Python by playing with new-style classes in Pure Python).

>>> class X(object):
...     def __imul__(self, x):
...         print 'imul'
>>> x = X()
>>> x *= 1

To cure this problem, all you need to do is upgrade your Python to version 2.2.1 or later.