Mark Young wrote:
I created a custom vector type, inheriting from object, and defined __mul__, __add__, etc. Unfortunately, the operators only work if I type "vector * (int/float/number)", in that exact order. My program produces an error if I instead type "number * vector".

Use __rmul__, __radd__, etc.

This makes sense to me, because I haven't told the number (int, float, whatever) how to deal with an object of type vector, (At least, I think that's why it doesn't work.). Is there any way to allow "number (operator) vector", short of modifying the standard types' behavior? Here's an example of the error.
vec1 = vector(5,6,7)
>>> vec1 * 2
(10, 12, 14)
>>> 2 * vec1
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    2 * vec1
TypeError: unsupported operand type(s) for *: 'int' and 'vector'
I'm using python 3.1.
Thanks.
------------------------------------------------------------------------

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to