deriving from sympy.Function

36 views
Skip to first unread message

Nico

unread,
Jul 14, 2015, 10:07:29 AM7/14/15
to sy...@googlegroups.com
This question is connected to my previous question [1].

I'd like to derive from `sympy.Function` and add some extra information to the class. This works:
```
import sympy
class MyTest(sympy.Function):
    def __init__(self, name):
        return
a = MyTest('a')
a.fun = lambda x: x
```
It'd be easier to add the function definition straight in the initializer. However
```
class MyTest(sympy.Function):
    def __init__(self, name, fun):
        self.fun = fun
        return
a = MyTest('a', lambda x: x)
```
does *not* work.

Any idea why that is and how to fix it?

Cheers,
Nico


Aaron Meurer

unread,
Jul 14, 2015, 11:51:44 AM7/14/15
to sy...@googlegroups.com
Generally of Function should override the class method eval, like

class MyClass(Function):
    @classmethod
    def eval(cls, *args):
        return an alternate object, or None to remain unevaluated

This automatically puts the *args in the object's .args.  If you want to handle non-SymPy objects like Python functions, you'll have to handle those outside of .args.

I think for what you want, you need to use __new__. Don't forget to use super() to call the superclass __new__ in your subclass.

Aaron Meurer

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/0b42776b-8a49-4d34-adaf-f722d6075a0f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages