Linked Questions
86 questions linked to/from What's the difference between eval, exec, and compile?
0
votes
1
answer
9k
views
Python 3 - exec() Vs eval() - Expression evaluation [duplicate]
After reading query.
below python code is still not clear,
>>> exec('print(5+10)')
15
>>> eval('print(5+10)')
15
In bash world,
exec replace the shell with the given command.
...
2
votes
1
answer
4k
views
what is the difference between eval and exec in python? [duplicate]
Possible Duplicate:
What’s the difference between eval, exec, and compile in Python?
I known that
eval is a function
exec is a statement
And the simple usage of both is :
eval('1+2')
exec 'print ...
2
votes
0
answers
1k
views
How to eval import in python? [duplicate]
I get the following error. Is there a way to eval import?
>>> eval('import os')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", ...
2
votes
1
answer
2k
views
using Python eval() Method to execute a string format function and return value [duplicate]
x = """
def test_add(a, b):
add = a + b
return add
test_add(10, 6)
"""
print(eval(x))
The above code produces following error:
File "<string>", ...
0
votes
1
answer
569
views
Print result if statement to exec is an expression [duplicate]
In python, if I exec a statement which prints a value, I will see the output, e.g., exec("print 10") gives me output 10. However, if I do exec("10") I got nothing as output, where as if I type 10 in ...
0
votes
1
answer
116
views
syntax error trying to eval code block containing assignment [duplicate]
I'm trying to convert English into a python code, and here is how I do it:
english.eng:
the following line is a comment: This is a demo of english programming language
set a new variable dorito as &...
0
votes
1
answer
149
views
Python convert a string in to its actual Boolean value? [duplicate]
I have a function that returns a string that can be interpreted as a Boolean
def foo():
return "not(True and False)"
How can I return the actual Boolean value, in this case True?
0
votes
0
answers
75
views
Python: eval( 'import re' ) is not working [duplicate]
I am trying to use eval to load. It does not work as in just running it. See Python shell:
The version is: python3.6m
>>>> eval( 'import re' )
Traceback (most recent call last):
File &...
0
votes
0
answers
74
views
python 3 syntax error while evaluating import line [duplicate]
Working on getting seriously into python3.
While trying to eval(generated_code) to parse arguments I get an unexpected syntax error during the eval. Iv'e whittled down the code to a simpler failure ...
0
votes
0
answers
38
views
Python: function returning exec is returning nothing [duplicate]
Here is my MWE (which looks stupid):
def somefunc(a0,a1,b1,a2,b2):
first = 'a0'
rest = ['+a{}+b{}'.format(i,i) for i in range(1, int((len(locals())-1) / 2)
for element in rest:
...
0
votes
0
answers
26
views
How to run a string with python [duplicate]
I'm trying to run a function that is inside a string variable in Python.
The code I'm using is:
def func1():
return "Hello"
word = "func1()"
a = exec(word)
print(a)
But, ...
0
votes
0
answers
25
views
Why does eval() give a SyntaxError on '='? [duplicate]
My Python 3 code:
test = "c.mode=0; c.preview=False"
eval(test)
Output:
File "<string>", line 1
c.mode=0; c.preview=False
^
SyntaxError: invalid syntax
Why? ...
55
votes
14
answers
41k
views
Choose Python function to call based on a regex
Is it possible to put a function in a data structure, without first giving it a name with def?
# This is the behaviour I want. Prints "hi".
def myprint(msg):
print msg
f_list = [ myprint ]
f_list[...
58
votes
6
answers
37k
views
Why doesn't exec work in a function with a subfunction?
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, ...