Python 使用带有自定义全局变量的 exec

标签 python shell exec

我想创建一个简单的 python shell。所以现在我只能执行代码。问题是,有没有办法使用自定义全局变量运行exec?谢谢

最佳答案

您可以传递自定义全局字典,如下所示:

a, b = 1, 2
exec('print(a+b)', {'a': 5, 'b': 5}) # prints 10

引用:The exec statement

关于Python 使用带有自定义全局变量的 exec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19195896/

相关文章:

python - Pandas .unique() TypeError : unhashable type: 'list'

python - 生成所有排列,其中排列长度 > Python 中元素的数量

windows - Visual Studio shell(独立 shell)有什么用?

bash - "set -euo pipefail"对 eval 有影响吗?

php - 从 PHP 运行 Python 脚本

python - 根据另一个数据帧的某些列值制作 Pandas 数据帧

java - Selenium 单击 chrome 物理按钮,如菜单、左导航、右导航、书签

macos - 我怎样才能让 iTerm 使用 brew 显示的更新版本的 bash?在 OSX 上更改用户的 shell

php - libreoffice 的 shell_exec 错误

go - exec: 在 $PATH 中找不到可执行文件