Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6161631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:30:41+00:00 2026-05-23T21:30:41+00:00

I have a string expression that I need to do eval on. This expression

  • 0

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!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:30:42+00:00Added an answer on May 23, 2026 at 9:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that I need to get a number from. this is
I have the following string expression in a PowerShell script: select count(*) cnt from
I have string that I want to chop to array of substrings of given
I have a String representation of a date that I need to create a
OK, this one is driving me nuts.... I have a string that is formed
I've spent some time, but still have to solution. I need regular expression that
I have a couple strings like: net_weight_(lbs) net_height_(inches) I need a regular expression that
I have a string that I need to edit, it looks something similar to
I need to create a custom Contains LINQ expression that derives from System.Linq.Expressions.Expression that
If I have a string with a valid math expression such as: String s

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.