为什么 exec("break") 在 while 循环中不起作用 [英] Why doesn't exec("break") work inside a while loop

查看:41
本文介绍了为什么 exec("break") 在 while 循环中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题所问,为什么下面的代码不起作用:

As the question asks, why doesn't the below code work:

while True:
      exec("break")

我正在通过 python 3.5.2 控制台在 pycharm 中执行上述操作.我最初认为这是上下文问题,但在阅读文档后,我还没有更深入地理解为什么会出现此错误.

I am executing the above in pycharm via python 3.5.2 console. I initially thought it was a context issue but after reading the documentation, I haven't come closer to understanding why this error ocurs.

SyntaxError: 'break' outside loop

提前致谢:)

顺便说一下,我知道它可以在没有 exec() 的情况下工作,我很好奇为什么它不能与 exec 一起工作(因为我的情况需要它)-欢迎提供全面的答案.

I understand that it works without exec() by the way, I'm curious why it won't work with exec (as my circumstances required it) - comprehensive answers welcome.

推荐答案

这是因为 exec() 对你周围的 while 循环一无所知.所以 exec() 在你的例子中看到的唯一语句是 break.不要使用 exec("break"),只需按原样使用 break.

This is because exec() is ignorant to your surrounding while loop. So the only statement that exec() sees in your example is break. Instead of using exec("break"), simply use break as is.

exec() 函数对其周围作用域的唯一访问权限是 globals()locals() 字典.exec() 的文档提供深入了解 exec() 的工作原理:

The only access the exec() function has to its surrounding scope, is the globals() and locals() dictionaries. The documentation for exec() provides some insight into how exec() works:

该函数支持Python代码的动态执行.object 必须是字符串或代码对象.如果是字符串,则该字符串将被解析为一组 Python 语句,然后执行该语句(除非发生语法错误).[1] 如果是代码对象,则简单执行.在所有情况下,执行的代码都应作为文件输入有效(请参阅参考手册中的文件输入"部分).请注意,即使在传递给 exec() 函数的代码上下文中,也不能在函数定义之外使用 return 和 yield 语句.返回值为 None.

This function supports dynamic execution of Python code. object must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). [1] If it is a code object, it is simply executed. In all cases, the code that’s executed is expected to be valid as file input (see the section "File input" in the Reference Manual). Be aware that the return and yield statements may not be used outside of function definitions even within the context of code passed to the exec() function. The return value is None.

在所有情况下,如果省略可选部分,则代码在当前范围内执行.如果只提供 globals,它必须是一个字典,它将用于全局和局部变量.如果给出了 globals 和 locals,则它们分别用于全局变量和局部变量.如果提供,locals 可以是任何映射对象.请记住,在模块级别,全局变量和本地变量是同一个字典.如果 exec 获得两个单独的对象作为全局对象和局部对象,则代码将被执行,就像它嵌入在类定义中一样.

In all cases, if the optional parts are omitted, the code is executed in the current scope. If only globals is provided, it must be a dictionary, which will be used for both the global and the local variables. If globals and locals are given, they are used for the global and local variables, respectively. If provided, locals can be any mapping object. Remember that at module level, globals and locals are the same dictionary. If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition.

如果 globals 字典不包含键 builtins 的值,则在该键下插入对内置模块 builtins 的字典的引用.通过这种方式,您可以通过在将自己的 builtins 字典传递给 exec() 之前将其插入到全局变量中来控制哪些内置函数可用于执行的代码.

If the globals dictionary does not contain a value for the key builtins, a reference to the dictionary of the built-in module builtins is inserted under that key. That way you can control what builtins are available to the executed code by inserting your own builtins dictionary into globals before passing it to exec().

这篇关于为什么 exec("break") 在 while 循环中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆