Created
December 17, 2016 15:17
-
-
Save belsander/d429328280240cba9b0991f38a5c42cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Basic examples | |
>>> var1 = 100 | |
>>> eval("50 + var1") | |
150 | |
>>> eval("var1 = 25") | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
File "<string>", line 1 | |
var1 = 25 | |
^ | |
SyntaxError: invalid syntax | |
>>> exec("var1 = 25") # returns None | |
>>> var1 | |
25 | |
# compile() examples | |
>>> exec(compile("var1 = 125", "<string>", "exec")) # returns None | |
>>> var1 | |
125 | |
>>> eval(compile("75", "<string>", "eval")) | |
75 | |
>>> exec(compile("75", "<string>", "eval")) # code object (generated by compile(), returns 75, but ignored by exec), so returns None | |
>>> eval(compile("75", "<string>", "exec")) # compile returns None, passed through by eval | |
>>> eval(compile("75", "<string>", "exec")) == None | |
True | |
>>> from dis import dis | |
>>> dis(compile("var1 = 125", "<string>", "exec")) | |
1 0 LOAD_CONST 0 (125) | |
3 STORE_NAME 0 (var1) | |
6 LOAD_CONST 1 (None) # here we see that None is returned | |
9 RETURN_VALUE | |
# A loop example with exec() | |
>>> exec("""for i in range(0,5): | |
... print('Test number: %s' % (i))""") | |
Test number: 0 | |
Test number: 1 | |
Test number: 2 | |
Test number: 3 | |
Test number: 4 | |
# Example with usage of a declared method | |
>>> def example_method(parameter): | |
... print("You gave parameter: %s" % (parameter)) | |
... return parameter + " rules!" | |
... | |
>>> var1 = example_method('Python') # This is an expression, so it both eval as exec should be able to process it | |
You gave parameter: Python | |
>>> exec("example_method('Python')") | |
You gave parameter: Python | |
>>> eval("example_method('Python')") | |
You gave parameter: Python | |
'Python rules!' | |
# Example with the usage of a while loop | |
>>> exec("""i = 0 | |
... while i < 3: | |
... print('Test number: %s' % (i)) | |
... i += 1""") | |
Test number: 0 | |
Test number: 1 | |
Test number: 2 | |
>>> eval("""i = 0 | |
... while i < 3: | |
... print('Test number: %s' % (i)) | |
... i += 1""") | |
Traceback (most recent call last): | |
File "<stdin>", line 4, in <module> | |
File "<string>", line 1 | |
i = 0 | |
^ | |
SyntaxError: invalid syntax | |
# Example of globals/locals to retrieve variable declared within exec() | |
>>> global_vars = dict() | |
>>> local_vars = dict() | |
>>> exec("global global_var1; global_var1, local_var1 = 100, 50", global_vars, local_vars) | |
>>> global_vars["global_var1"] | |
100 | |
>>> local_vars["local_var1"] | |
50 | |
>>> global_var1 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
NameError: name 'global_var1' is not defined | |
>>> local_var1 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
NameError: name 'local_var1' is not defined | |
# Timeit time! | |
$ python -m timeit -s 'var1 = 100; var2 = 50; var1 * var2' | |
100000000 loops, best of 3: 0.00785 usec per loop | |
$ python -m timeit -s "code_to_exec_compiled = compile('var1 = 100; var2 = 50; var1 * var2', '<string>', 'exec')" "exec(code_to_exec_compiled)" | |
1000000 loops, best of 3: 0.425 usec per loop | |
$ python -m timeit -s "code_to_exec = 'var1 = 100; var2 = 50; var1 * var2'" "exec(code_to_exec)" | |
100000 loops, best of 3: 9.33 usec per loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment