Why I can call 'print' from 'eval'

前端 未结 4 609
夕颜
夕颜 2021-02-06 09:15

For code:

#!/usr/bin/python

src = \"\"\"
print \'!!!\'
import os
\"\"\"

obj = compile(src, \'\', \'exec\')
eval(obj, {\'__builtins__\': False})
4条回答
  •  逝去的感伤
    2021-02-06 09:49

    import calls the global/builtin __import__ function; if there isn't one to be found, import fails.

    print does not rely on any globals to do its work. That is why print works in your example, even though you do not use the available __builtins__.

提交回复
热议问题