How to avoid warning on unused local variable when using eval() in Python
Clash Royale CLAN TAG#URR8PPP
How to avoid warning on unused local variable when using eval() in Python
I have a function to take in input argument to either solve or get the expressions as output to be printed into a text file.
In the function itself, I have a lot of assigned local variables to be used in the expressions and using eval() to solve the expressions. All the expressions are given in string format. The reason for doing that is to avoid typing the complex expressions two times (solving & printing as output)
Is there a better way to implement this in order to avoid the warning of "local variable is assigned to but never used" for all the assigned variables?
Thank you.
Here is a simple example of my code, I have more complicated expression in the real case: def TestEval(state= 'solve'): x = 2 y = 4 dx = '3*x-4*y' if state == 'solve': return print('expression: ', dx) elif state == 'get': return print('value: ', eval(dx)) else: print('Error')
– JWY
Aug 10 at 3:28
Please don't put codes in the comments because it's not readable. You can click the edit link to put that code in the post itself. Make sure to format it as code by highlighting it and clicking the
button).– Gino Mempin
Aug 10 at 3:31
@JWY Post stack trace as well.
– Upasana Mittal
Aug 10 at 4:36
1 Answer
1
Python isn't giving you this warning; some linter or other static analyzer is, because you're doing something unusual.
Now, you know you're doing something unusual that your static analyzer doesn't understand and is going to complain about. If you know that, and still want to do it, the right thing to do is to tell the analyzer to skip over that code, or to skip certain checks on it.
But of course each analyzer has its own way of doing that, and without you telling us which one you're using, or even giving up the complete message so we could at least search or guess, it's impossible to tell you what fix you want. But if you skim the docs for whatever you're using, it should be very easy to find.
Just as an example, if you're using flake8
as a wrapper around pyflakes
, and the warning is a pyflakes
F841, you can add a # noqa: F841
comment to the offending line to silence warning. If you don't find that obvious enough, also add a comment explaining that your variables appear unused but actually are being used in the eval
; that should be more than enough to make any reader (even you, six months from now, when you've completely forgotten this code but have to debug it) as happy as flake8
.
flake8
pyflakes
pyflakes
# noqa: F841
eval
flake8
A different alternative is to use an object that represents your equations, that knows how to represent itself as a string, and how to evaluate a value given values for the variables, and how to build up new objects when used with mathematical operators.
There’s a great library called SymPy that can do all of this and more. You would need to reorganize your code a bit to make this work, defining x
and y
as SymPy variables and numerically evaluating your equation by passing it substitutions for those variables, instead of making them normal Python numbers. But if that fits with what you want, using SymPy equations for your equations is probably better than using strings.
x
y
And it will also make your linter happy, because you’re now building equations out of things like x + y
, which clearly use the variables, instead of strings like 'x + y', which only use them indirectly by being passed to
eval`.
x + y
, which only use them indirectly by being passed to
Thank you for your suggestion. I wonder if there is better way to code that other than turning off the warning? Thank you.
– JWY
Aug 10 at 3:56
@JWY The ideal solution for something like this is probably using a symbolic equations library, like SymPy.
– abarnert
Aug 10 at 4:23
Thank you so much for your help! I have tried with SymPy and it works for my case.
– JWY
Aug 10 at 6:37
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Please show us what you have done till now.
– Upasana Mittal
Aug 10 at 3:24