| I have the following simple function 'add4' stored in a file 'foo.sage': I can use this in sage by: How can I change my function to a method so that I can call it using the x.add4() syntax? I still want the method to reside in an external file. |
Asked: Nov 16 '11
Seen: 59 times
Last updated: Nov 16 '11
powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.
Methods are "subfunctions" of objects, though. What object in particular do you want to attach your function to?
DSM (Nov 16 '11)Is it possible to 'attach' it to SageObject or do I have to be more specific? My example is simple but later i would want to do more complicated actions.
rtrwalker (Nov 16 '11)No, I don't think you can do that: SageObject isn't that kind of parent. Frankly, without patching Sage, it's hard enough even to get subclassing Expression to behave nicely. (See, e.g., http://groups.google.com/group/sage-support/browse_thread/thread/f749ba3cd079c6f4?pli=1).
DSM (Nov 16 '11)Thanks for the link. The material is beyond my current ability so I think I'll just stick to functions.
rtrwalker (Nov 16 '11)Sure. FYI, though, if you do have a particular new object you want, then turning a function into a method is trivial (you simply put the function in the class and add a new argument traditionally called "self".) So if you hit any problems along the way, just describe what your goal is and one of the regulars will be happy to help figure out a way to get there. (It could be that a class/method solution is the way to go, but that would require more details.)
DSM (Nov 16 '11)