On Jul 11, 2008, at 7:19 AM, David Bate wrote: > Hello, > I am a beginner to sage-devel, but I have done a search and nothing > like this came up, although I believe it should be a newbie question. > > I have a class, my_class say derived from float, that I want to put > into sage so that one can type 3*my_instance on the command prompt and > something sensible will happen. However, rather than looking for > __mul__ in type sage.rings.integer.Integer, noticing that it doesn't > work for my_class and then trying __rmul__ in my_class (which from my > knowledge of python I would expect to happen) it appears sage casts > my_instance to a float and tries the multiplication that way. > > To solve this I tried to write a wrapper for > sage.rings.integer.Integer.__mul__ however I get the error "can't set > attributes of built-in/extension type 'sage.rings.integer.Integer'" > when executing > sage: sage.rings.integer.Integer.__mul__ = my_new_multiplication > > Could someone enlighten me about either problem, and a possible way to > fix it. > > Thanks, > David.
This will only work for Python instances. sage.rings.integer.Integer is an extension class, just like the builtin Python int, and the error is similar: sage: int.__mul__ = my_new_multiplication ------------------------------------------------------------ Traceback (most recent call last): File "<ipython console>", line 1, in <module> TypeError: can't set attributes of built-in/extension type 'int' Also, note that sage.rings.integer.Integer itself is highly optimized with lots of hacks (since its such basic type) so trying to extend it may be difficult. - Robert --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@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-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---