0

Is there a way to take a string and use it as a command in Python?

Something similar to the example shown below.

x = "import time"

x

time.sleep(1)

I know the example above won't work, that is just to help you understand what I am trying to do.

jumbi533
  • 61
  • 1
  • 2
  • 9
  • 1
    What are you really trying to do? –  Mar 24 '15 at 16:56
  • 3
    Simple answer: yes. Better answer: yes, but you probably don't actually want to. – Eric Hughes Mar 24 '15 at 16:59
  • I was trying to create a program the seems like artificial intelligence (it's really not) that helps me out with my computer and can learn new things and create new functions based on what it has learned. I mean like the 'def' function, I wanted my program to be able to define new functions with the 'def' command, and the name of that definition is going to be the string. – jumbi533 Mar 24 '15 at 17:00
  • A better example would be something like this. x = "def name():" x – jumbi533 Mar 24 '15 at 17:02
  • if you are trying to write a program that can write its own code ... that is not *like* AI... that is AI ... and very complicated stuff... if thats not really what you are trying to do you should come up with a better way of doing this than eval and exec – Joran Beasley Mar 24 '15 at 17:21
  • I did consider writing the script so the program saves everything into a text file and works from there. The thing is though, I have already made it so the program can rewrite itself, although if it were to create anoth 'def' then it would need to (obviously) make a new 'def' function that was not named the same name as the others, and the easiest way to do that is with strings. – jumbi533 Mar 24 '15 at 17:35

2 Answers2

3

The dangerous but powerful exec() and eval() are what you're looking for:

What does Python's eval() do?

Running Python code contained in a string

Use exec()(its a function in Python 3.x, but a statement in 2.x) to evaluate statements, so:

exec('print(5)')           # prints 5.

and eval() to evaluate expressions:

x=5                        #prints 5
eval('x+1')                #prints 6
Community
  • 1
  • 1
Luke
  • 5,567
  • 4
  • 37
  • 66
  • This should be a comment, not an answer. –  Mar 24 '15 at 16:59
  • it is a direct answer to his question. I think this should be an answer, but I don't think the question should exist. – Luke Mar 24 '15 at 16:59
  • An answer that includes only a link is not an answer here in SO. Include a code snippet to improve your answer. –  Mar 24 '15 at 17:00
  • @LutzHorn why? It answers the question fully and succinctly in the text, warns about the dangers of using `eval`, and links to more (but critically: OPTIONAL) information – Adam Smith Mar 24 '15 at 17:00
  • @LutzHorn The link is irrelevant to this answer. – Adam Smith Mar 24 '15 at 17:00
  • 2
    It shouldn't have even been answered, there are plenty of duplicates ... http://stackoverflow.com/questions/1015142/running-python-code-contained-in-a-string – Matt Mar 24 '15 at 17:02
  • I apologize if my question is in any way confused. – jumbi533 Mar 24 '15 at 17:04
3

You can use eval.eval() is used to evaluate expression, If you want to execute a statement, use exec()

See example for eval:

def fun():
    print "in fun"

eval("fun()")

x="fun()"
eval(x)

See example for exec.

exec("print 'hi'")
vks
  • 67,027
  • 10
  • 91
  • 124