Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Python Compiler and Virtual Machine handle eval expressions?

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?

like image 290
rmesteves Avatar asked Apr 18 '26 04:04

rmesteves


1 Answers

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.

like image 77
Barmar Avatar answered Apr 22 '26 00:04

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!