Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to use vars()

I have a string expression that I need to do eval on. This expression may have key names from a given dictionary and values for those keys may or may not be strings. For my eval to be able to evaluate those names, I am creating variables with the key names that appear in that dict and assign it the value from the dict as in the example below. But from what I have read here, vars() should nto be used. This has made me worry about the stability as well as risks of what I have implemented. Any thoughts or suggestions to how to implmenet this in a better way?

def test(e1,d1):
 for k,v in d1.iteritems():
     if k in e1:
        vars()[k]=v
 return eval(e1)

test('x+y', {'x':1, 'y':2})

Thanks!!

like image 926
Rinks Avatar asked Dec 06 '25 18:12

Rinks


1 Answers

There is a better way to do what you're trying to do: the optional globals and locals arguments to eval.

def test(e1, d1):
    return eval(e1, globals(), d1)

does the same thing as your code but without needing to muck with vars. If you can get away with passing an empty dictionary as the second argument instead of globals(), that will insulate you from side effects of e1.

like image 60
zwol Avatar answered Dec 08 '25 07:12

zwol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!