exec

Behavior of exec function in Python 2 and Python 3

Behavior of exec function in Python 2 and Python 3 Question: Following code gives different output in Python2 and in Python3: from sys import version print(version) def execute(a, st): b = 42 exec(“b = {}nprint(‘b:’, b)”.format(st)) print(b) a = 1. execute(a, “1.E6*a”) Python2 prints: 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] …

Total answers: 4

Why doesn't an import in an exec in a function work?

Why doesn't an import in an exec in a function work? Question: I can put an import statement in a string, exec it, and it works (prints a random digit): code = “”” import random def f(): print random.randint(0,9) “”” def f(): pass exec code f() Now, if I put exec code and f() in …

Total answers: 3

How to pass arguments to the __code__ of a function?

How to pass arguments to the __code__ of a function? Question: The following works: def spam(): print “spam” exec(spam.__code__) spam But what if spam takes arguments? def spam(eggs): print “spam and”, eggs exec(spam.__code__) TypeError: spam() takes exactly 1 argument (0 given) Given, that I don’t have access to the function itself but only to a …

Total answers: 6

Why doesn't exec work in a function with a subfunction?

Why doesn't exec work in a function with a subfunction? Question: It looks like you can’t use exec in a function that has a subfunction… Anyone know why this Python code doesn’t work? I get an error at the exec in test2. Also, I know exec’s aren’t good style, but trust me, I’m using exec …

Total answers: 6

python: get the print output in an exec statement

python: get the print output in an exec statement Question: I want to get the output of an exec(…) Here is my code: code = """ i = [0,1,2] for j in i : print j """ result = exec(code) How could I get the things that print outputed? How can I get something like: …

Total answers: 7

What's the difference between eval, exec, and compile?

What's the difference between eval, exec, and compile? Question: I’ve been looking at dynamic evaluation of Python code, and come across the eval() and compile() functions, and the exec statement. Can someone please explain the difference between eval and exec, and how the different modes of compile() fit in? Asked By: andrewdotnich || Source Answers: …

Total answers: 3

Cannot change global variables in a function through an exec() statement?

Cannot change global variables in a function through an exec() statement? Question: Why can I not change global variables from inside a function, using exec()? It works fine when the assignment statement is outside of exec(). Here is an example of my problem: >>> myvar = ‘test’ >>> def myfunc(): … global myvar … exec(‘myvar …

Total answers: 4

How to get local variables updated, when using the `exec` call?

How to get local variables updated, when using the `exec` call? Question: I thought this would print 3, but it prints 1: # Python3 def f(): a = 1 exec("a = 3") print(a) f() # 1 Expected 3 Asked By: ubershmekel || Source Answers: This issue is somewhat discussed in the Python3 bug list. Ultimately, …

Total answers: 3

How to spawn parallel child processes on a multi-processor system?

How to spawn parallel child processes on a multi-processor system? Question: I have a Python script that I want to use as a controller to another Python script. I have a server with 64 processors, so want to spawn up to 64 child processes of this second Python script. The child script is called: $ …

Total answers: 4

How do I execute a string containing Python code in Python?

How do I execute a string containing Python code in Python? Question: How do I execute a string containing Python code in Python? Do not ever use eval (or exec) on data that could possibly come from outside the program in any form. It is a critical security risk. You allow the author of the …

Total answers: 14