Dear all,

i just found that there is a difference how the __rmul__ method works
for usual classes and for cdefined classes. A short example (yes,
usually i don't want that __mul__ changes self...):

cdef class MulC:
    cdef object r
    def __init__(self,r):
        self.r = r
    def __repr__(self):
        return str(self.r)
    def __mul__(MulC self,s):
        self.r=self.r*s
        return self
    def __rmul__(MulC self,s):
        self.r=self.r*s
        return self

class MulP:
    def __init__(self,r):
        self.r = r
    def __repr__(self):
        return str(self.r)
    def __mul__(self,s):
        self.r=self.r*s
        return self
    def __rmul__(self,s):
        self.r=self.r*s
        return self

and now:
sage: M=MulP(3)
sage: M*3
9
sage: 3*M
27
This is the behaviour that i want.

But for the cdefined class i get a traceback:
sage: M=MulC(3)
sage: M*3
9
sage: 3*M
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call
last)
...
TypeError: unsupported operand parent(s) for '*': 'Integer Ring'
and ...


The Traceback mentions something about coercion. So, is it true that
3*M tries coercion, if M is a cdefined class, but tries to apply
M.__rmul__(3) if M is a python class? Why? Is there a way to work
around coercion in this case?

Yours
      Simon

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to