python - How does one check if a variable is a sympy expression? -
i wanted check python variable sympy expression. easy check if sympy variable with:
isinstance(arg, symbol.symbol)
but can't find how do:
isinstance(arg, sympy.expression)
is possible check if python variable holds sympy expression or variable?
as quick check did:
expr2 = x-y type(expr2) <class 'sympy.core.add.add'>
but don't want have giant series if statement clause checking each possible type of maths expression. seems redundant/silly.
it nice able detect when variable of type of sympy related thing , act on (and maybe later check if expression or more detailed...)
i think need sympy.expr
instead of sympy.expression
:
in [164]: expr2 out[164]: x - y in [165]: type(expr2) out[165]: sympy.core.add.add
but if @ __bases__
of type:
in [166]: type(expr2).__bases__ out[166]: (sympy.core.expr.expr, sympy.core.operations.assocop)
and so:
in [167]: isinstance(2, sympy.expr) out[167]: false in [168]: isinstance(x, sympy.expr) out[168]: true in [169]: isinstance(x-y, sympy.expr) out[169]: true
Comments
Post a Comment