1

I have a string containing some if/else statements, for example:

self.y = 10

x = "if 1 > self.y: DO-Something else: Do-something-else"

is it possible to run this code while the program is opperating. (pseudocode explination)

x = "if 1 > self.y: DO-Something else: Do-something-else"

runprogram(x)

Thanks!

1
  • exec() will run code :). but the use of it is usually a big NO NO. see here for more information stackoverflow.com/questions/4158117/… but I can sum it up in two words "security issues"! Commented Apr 4, 2014 at 8:14

2 Answers 2

1

use exec to execute your code in string.

But use exec with care :)

Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned in the comments, you can use exec (eval would do too, in some situations).

But you need to be very careful, especially if the code to be evaluated (or executed) will be input by the users.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.