#6245: make a custom infix operator decorator
-------------------------------------------------------+--------------------
   Reporter:  jason                                    |       Owner:  cwitty   
   
       Type:  enhancement                              |      Status:  
needs_review
   Priority:  major                                    |   Milestone:  sage-4.4 
   
  Component:  misc                                     |    Keywords:           
   
     Author:  Jason Grout, Carl Witty, Florent Hivert  |    Upstream:  N/A      
   
   Reviewer:                                           |      Merged:           
   
Work_issues:                                           |  
-------------------------------------------------------+--------------------

Comment(by jason):

 Replying to [comment:14 rossk]:
 > Noticed references to {{{__rmul__}}} in the code so I thought I might
 try setting up a situation[[BR]]
 >
 > where {{{__mul__}}} fails so {{{__rmul__}}} would be exercised. In
 setting up the test code, I got an[[BR]]
 >
 > error but cant see whats wrong (any thoughts?)


 The problem is that your __mul__ function does not check its arguments.
 Note that:

 {{{
 sage: Fraction(2,3)*matrix(2,1,[1,2])
 Traceback (click to the left of this block for traceback)
 ...
 AttributeError: 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'
 object has no attribute 'numerator'
 }}}

 Instead, you should check your arguments in the __mul__ function before
 blindly calling the .numerator method, or at least you should catch the
 error, like this:

 {{{
 class Fraction:
   def __init__(self, numerator, denominator=1):
     self.numerator = numerator
     self.denominator = denominator
   def __str__(self):
     return "Fraction(%d,%d)" % (self.numerator, self.denominator)
   __repr__=__str__
   def zmul(self, other):
     return Fraction(self.numerator*other.numerator,
                   self.denominator*other.denominator)
   def __mul__(self, other):
     try:
         return Fraction(self.numerator*other.numerator,
                   self.denominator*other.denominator)
     except:
         return NotImplemented

 }}}

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/6245#comment:15>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to