
파이썬 내장 함수로 문자열로 표현된 것을 실행하는 것은 동일하지만 사용 용도에 차이가 있다.
핵심
eval() 은 single expression 을 사용 -> expression 결과를 리턴
exec() 는 multi statement 을 사용 -> expression 결과가 아닌 None 값을 리턴
둘다 현재 python 런타임에서 실행되어 반영된다.
1. eval(): This function is used to evaluate a single expression dynamically. It takes a string as input, which should be a valid Python expression, and executes it. It returns the result of the expression. For example, eval('2 + 3') would return 5.
2. exec(): This function is used to execute a block of dynamically constructed Python code. It takes a string or code object as input and executes it as Python code. Unlike eval(), exec() can handle multiple statements and code blocks. It doesn't return any value explicitly; rather, it executes the code within the provided string or code object.
tips. Statement Vs Expression
Statements are often used to control the flow of a program and manipulate data.
Expressions are typically used to represent computations and produce results.
statement ex) a=1
-> 변수에 할당하거나 for, while, if-else 같이 프로그램의 흐름을 조절하거나 데이터를 조작하는 것
expression ex) 1+2
-> 계산을 표현하거나 결과를 생성하는 것
Python 공식 문서 eval 설명

Python eval 소스 코드
static PyObject *
builtin_eval(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
PyObject *source;
PyObject *globals = Py_None;
PyObject *locals = Py_None;
if (!_PyArg_CheckPositional("eval", nargs, 1, 3)) {
goto exit;
}
source = args[0];
if (nargs < 2) {
goto skip_optional;
}
globals = args[1];
if (nargs < 3) {
goto skip_optional;
}
locals = args[2];
skip_optional:
return_value = builtin_eval_impl(module, source, globals, locals);
exit:
return return_value;
}
Python 공식 문서 exec 설명

Python eval 소스 코드
static PyObject *
builtin_exec(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
#define NUM_KEYWORDS 1
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(closure), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
static const char * const _keywords[] = {"", "", "", "closure", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "exec",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[4];
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
PyObject *source;
PyObject *globals = Py_None;
PyObject *locals = Py_None;
PyObject *closure = NULL;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
if (!args) {
goto exit;
}
source = args[0];
if (nargs < 2) {
goto skip_optional_posonly;
}
noptargs--;
globals = args[1];
if (nargs < 3) {
goto skip_optional_posonly;
}
noptargs--;
locals = args[2];
skip_optional_posonly:
if (!noptargs) {
goto skip_optional_kwonly;
}
closure = args[3];
skip_optional_kwonly:
return_value = builtin_exec_impl(module, source, globals, locals, closure);
exit:
return return_value;
}
참고
python official function reference (eval)
https://docs.python.org/ko/3/library/functions.html#eval
python official function reference (exec)
https://docs.python.org/ko/3/library/functions.html#exec
python source code
https://github.com/python/cpython