简体   繁体   中英

sympy: sometimes getting 'Integer' object has no attribute 'strip' after using parse_expr

After using parse_expr like this:

s = parse_expr(s, transformations=standard_transformations + (implicit_multiplication_application,))

Where s is different kinds of math (ie. 3+2 or 2x+5). The problem is when s is just a integer/float the function results in "'Integer' object has no attribute 'strip'"

My workaround is just to check if s is a number. I'm just wondering if there is a way to safely use parse_expr without it crashing for integers.

Update: had a glitch with my server so casting to str didn't actually cast. Restarted the server and now casting to string works:

s = str(s)

Docstring:

Converts the string ``s`` to a SymPy expression, in ``local_dict``

Parameters
==========

s : str
    The string to parse.

s has to be a string.

Always casting to string is better than checking for numbers. So if your getting the same error just do: s = str(s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM