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

查看:112
本文介绍了为什么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代码.对象必须是字符串或代码对象.如果是字符串,则将字符串解析为一组Python语句,然后执行该语句(除非发生语法错误). [1]如果它是代码对象,则只需执行即可.在所有情况下,执行的代码都可以作为文件输入有效(请参见《参考手册》中的文件输入"部分).请注意,即使在传递给exec()函数的代码上下文中,也不能在函数定义之外使用return和yield语句.返回值为无.

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.

在所有情况下,如果省略了可选部分,则代码将在当前作用域中执行.如果仅提供全局变量,则必须为字典,该字典将用于全局变量和局部变量.如果给出了全局变量和局部变量,则它们分别用于全局变量和局部变量.如果提供的话,本地变量可以是任何映射对象.请记住,在模块级别,全局变量和局部变量是相同的字典.如果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 的值,则在该键下插入对内置模块buildins的词典的引用.这样,您可以通过将自己的 builtins 字典插入全局变量中,然后再将其传递给exec(),来控制执行的代码可以使用哪些buildins.

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天全站免登陆