博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python函数专讲:exec执行函数
阅读量:2381 次
发布时间:2019-05-10

本文共 2331 字,大约阅读时间需要 7 分钟。

今天的Python学习教程想跟大家说一下exec执行函数

python函数专讲:exec执行函数

exec 函数功能:执行储存在字符串或文件中的 Python 语句,相比于 eval,exec可以执行更复杂的 Python 代码。

英文解释

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). 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.

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.

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().

注意: 在 Python2 中exec不是函数,而是一个内置语句(statement),但是Python 2中有一个 execfile() 函数。可以理解为 Python 3 把 exec 这个 statement 和 execfile() 函数的功能够整合到一个新的 exec() 函数中去了。

所以类似功能的函数在python2中是execfile。

适用版本

Python3.x

语法

以下是 exec 的语法:

exec(object[, globals[, locals]])

参数

object:必选参数,表示需要被指定的Python代码。它必须是字符串或code对象。如果object是一个字符串,该字符串会先被解析为一组Python语句,然后在执行(除非发生语法错误)。如果object是一个code对象,那么它只是被简单的执行。

globals:可选参数,表示全局命名空间(存放全局变量),如果被提供,则必须是一个字典对象。

locals:可选参数,表示当前局部命名空间(存放局部变量),如果被提供,可以是任何映射对象。如果该参数被忽略,那么它将会取与globals相同的值。

返回值

exec 返回值永远为 None。

实例

>>>exec 'print "Hello Python"'Hello Python# 单行语句字符串>>> exec "print 'pythontab.com'"pythontab.com # 多行语句字符串>>> exec """for i in range(5):... print "iter time: %d" % i... """iter time: 0iter time: 1iter time: 2iter time: 3iter time: 4

转载地址:http://rekab.baihongyu.com/

你可能感兴趣的文章
数据预处理速度高倍提升,3行python代码简单搞定!
查看>>
零基础如何迅速学习python?
查看>>
同样是学习Python的程序员,为什么他却可以用Python两年躺赚200W
查看>>
python基础题目大全,测试你的水平,巩固知识(含答案)
查看>>
Python用了这么多年,总结出超实用的功能和特点
查看>>
Python爬虫入门,8个常用爬虫技巧盘点
查看>>
Python轻松查看微信撤回消息,秘密无处可藏
查看>>
国外Python黑客技术,攻击自动化玩得真6
查看>>
最后悔的是遇见了Python!
查看>>
前端程序员市场分析:前面是火海,后面是刀山,走还是不走?
查看>>
做程序员一般都需要什么学历?大厂招不招低学历?你想知道的都在这里了
查看>>
零基础html5网站开发学习步骤方法
查看>>
程序员真实生活曝光,蹦迪也带着电脑,网友解释:这样才有安全感!
查看>>
CSS的23个垂直居中技巧,你都学会了吗?
查看>>
黑客攻击用的最短代码大揭秘,颠覆你的世界观!
查看>>
零基础的自学前端之路,当年的入坑之旅
查看>>
新手程序员?教你解决办法!基础都掌握了,动手敲代码就一脸懵逼
查看>>
程序员快速进阶学习到底要看书还是要看视频?
查看>>
web游戏框架哪家强?国内外精选优质框架分析及注意事项
查看>>
各行业都爱用什么编程语言开发?
查看>>