Search code examples
pythonsympyevaluate

I set evaluate=False, buy SymPy's parse_expr still evaluates the expression


I'm trying not to evaluate the expression, but it's being evaluated anyway.

This: parse_expr("sqrt(I**2)", evaluate=False)

Returns this: i

Why is this happening? Is there a way to change it?


Solution

  • Rather than relying on the flag, try parse the string in a context:

    >>> from sympy.core.parameters import evaluate
    >>> with evaluate(0):
    ...  parse_expr('sqrt(I**2)')
    ...
    sqrt(I**2)
    >>> _.args
    (I**2, 1/2)