On 9/4/10 1:35 AM, ross kyprianou wrote:
Thanks guys

exp, log, sqrt etc covers well over 50% of the requirement so Im really
ahead - Once again - THANKS!

It seems functional.py is definitely the place to be looking at. Whats
needed is a "catch-all/anonymous" function in functional.py so that
X.myfunction() is "try'ed" whenever myfunction(X) is called (much like
exp(X) invokes X.exp() in functional.py)
(can lambda functions or __call__ help here?)

Anyone have any ideas what mechanism might do that?



Are you suggesting something like:

http://www.sagemath.com/doc/reference/sagenb/misc/support.html#sagenb.misc.support.automatic_names

which takes any function foo(bar, ...) and tries to call bar.foo(...)?

I think there are good reasons that automatic_names is off by default. If I mistype a function name, for example, I want to know about it, rather than it trying to magically call a method.

Another thing that would clean up the code in misc/functional.py is a decorator for trying the method first. For example:

def try_method_first(f):
    from misc import sage_wraps
    @sage_wraps(f)
    def new_f(x,*args,**kwds):
        if hasattr(x,f.func_name):
            return getattr(x,f.func_name)(*args,**kwds)
        else:
            return f(x,*args,**kwds)
    return new_f

Then we could write code something like this:

sage: from sage.misc.decorators import try_method_first
sage: @try_method_first
....: def my_new_function(x,a,b=2):
....:     print "in function", x, a, b
....:
sage: class my_specialized_object:
....:     def my_new_function(*args,**kwds):
....:         print "in class's specialized method", args, kwds
....:
sage: my_new_function(2,3)
in function 2 3 2
sage: C=my_specialized_object()
sage: my_new_function(C,3)
in class's specialized method (<__main__.my_specialized_object instance at 0x10045a908>, 3) {}


Notice that it called the method if it existed, but otherwise did whatever the function normally does.

Thanks,

Jason

--
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to