#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 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?)
 {{{
 class Fraction:
   def __init__(self, numerator, denominator=1):
     self.numerator = numerator
     self.denominator = denominator
   def __str__(self):
     return "%d/%d" % (self.numerator, self.denominator)
   def zmul(self, other):
     return Fraction(self.numerator*other.numerator,
                   self.denominator*other.denominator)
   def zrmul(self, other):
     return Fraction(other*self.numerator,
                   self.denominator)
   def __mul__(self, other):
     return Fraction(self.numerator*other.numerator,
                   self.denominator*other.denominator)
   def __rmul__(self, other):
     return Fraction(other*self.numerator,self.denominator)

 # multiplication operator using methods
 print Fraction(2,3).zmul(Fraction(5,4))
 }}}
 10/12
 {{{
 # multiplication infix operator
 def dot(a,b):
   return a.zmul(b)
 dot = infix_operator('multiply')(dot)
 print Fraction(2,3) *dot* Fraction(5,4)
 }}}
 crashes with
 {{{
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "_sage_input_59.py", line 9, in <module>
     open("___code___.py","w").write("# -*- coding: utf-8 -*-\n" +
 
_support_.preparse_worksheet_cell(base64.b64decode("Z...<abbreviated>...Y="),globals())+"\n");
 execfile(os.path.abspath("___code___.py"))
   File "", line 1, in <module>

   File "/tmp/tmpIR_EGe/___code___.py", line 8, in <module>
     u *dot* v
   File "", line 1, in <module>

   File "", line 13, in __mul__

 AttributeError: dot instance has no attribute 'numerator'
 }}}

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/6245#comment:14>
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