Warm tip: This article is reproduced from serverfault.com, please click

How does Python Compiler and Virtual Machine handle eval expressions?

发布于 2020-11-30 23:58:48

Lets suppose I have a code in Python and in the middle of this code I have an eval expression which depends on some external input, for example:

exp = raw_input() 
eval(exp)

My question is: which mechanism Python's compiler and interpreter use to bind the code which can be compiled with the code that depends on a runtime value?

Questioner
rmesteves
Viewed
0
Barmar 2020-12-01 08:37:24

The Python compiler doesn't care where the code it's compiling comes from. It can compile code that comes from a file before execution when you run python filename, it can compile files during execution when you use import, and it can compile code from a string expression when you call eval() or exec(). These functions invoke the compiler dynamically.