Your somefunc is sympified as Function('somefunc'), so you could do

somefunc = Function('somefunc')
if expr.func == somefunc:
    ...

If you want to test for undefined functions generically you can use

from sympy.core.function import UndefinedFunction
if isinstance(expr.func, UndefinedFunction):
    ...

Aaron Meurer

On Fri, Aug 5, 2022 at 12:40 PM Anton Makarov <[email protected]> wrote:
>
> Hi there.
> I am trying to test the root of expression tree, for example:
> expr = sympify("somefunc(2-5)")
> if expr.func.name =="somefunc":
>     call_func_1()
> else:
>     call_func_2()
>
> It works just fine, but when I change the expression, for example, to
> expr = sympify("5+6")
>
> Code throws the exception, with error:
> AttributeError: type object 'Add' has no attribute 'name'
>
> I don't understand, why Add class has no attribute "name". Or what attribute 
> has all the classes, so I can test the head of any expression without code 
> exception?
>
> What code should I write ?
>
> --
> 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 [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/0987ccd8-66d0-468b-ab1c-9035732cae11n%40googlegroups.com.

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6K6xgJwqkmiQ6xPR%3D7noO8_omy65Uw7YKVAKhM%2Br5T-dA%40mail.gmail.com.

Reply via email to