Python内置函数compile
英文文档:
compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
- Compile the source into a code or AST object. Code objects can be executed by
exec()oreval(). source can either be a normal string, a byte string, or an AST object. Refer to theastmodule documentation for information on how to work with AST objects. - The filename argument should give the file from which the code was read; pass some recognizable value if it wasn’t read from a file (
'<string>'is commonly used). - The mode argument specifies what kind of code must be compiled; it can be
'exec'if source consists of a sequence of statements,'eval'if it consists of a single expression, or'single'if it consists of a single interactive statement (in the latter case, expression statements that evaluate to something other thanNonewill be printed). - The optional arguments flags and dont_inherit control which future statements (see PEP 236) affect the compilation of source. If neither is present (or both are zero) the code is compiled with those future statements that are in effect in the code that is calling
compile(). If the flagsargument is given and dont_inherit is not (or is zero) then the future statements specified by the flags argument are used in addition to those that would be used anyway. If dont_inherit is a non-zero integer then the flags argument is it – the future statements in effect around the call to compile are ignored. - Future statements are specified by bits which can be bitwise ORed together to specify multiple statements. The bitfield required to specify a given feature can be found as the
compiler_flagattribute on the_Featureinstance in the__future__module. - The argument optimize specifies the optimization level of the compiler; the default value of
-1selects the optimization level of the interpreter as given by-Ooptions. Explicit levels are0(no optimization;__debug__is true),1(asserts are removed,__debug__is false) or2(docstrings are removed too). - This function raises
SyntaxErrorif the compiled source is invalid, andValueErrorif the source contains null bytes. - If you want to parse Python code into its AST representation, see
ast.parse(). - Note: When compiling a string with multi-line code in
'single'or'eval'mode, input must be terminated by at least one newline character. This is to facilitate detection of incomplete and complete statements in thecodemodule. - Changed in version 3.2: Allowed use of Windows and Mac newlines. Also input in
'exec'mode does not have to end in a newline anymore. Added the optimize parameter. - Changed in version 3.5: Previously,
TypeErrorwas raised when null bytes were encountered in source. - 说明:
- 1,将source编译为代码或者AST对象,代码对象能够通过exec语句来执行或者eval()进行求值
- 2,参数source:字符串或者AST(Abstract Syntax Trees)对象,即需要动态执行的代码段
- 3.,参数filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值,当传入source参数时,filename参数传入空字符即可
- 4,参数mode:指定编译代码的种类,可以指定为'exec', 'eval', 'single',当source中包含流程语句时,mode应指定为exec;当source中只包含一个简单的求值表达式,mode为eval;当source包含交互式命令语句,mode为single
>>> #流程语句使用exec
>>> code1 = 'for i in range(0,10): print (i)'
>>> compile1 = compile(code1,'','exec')
>>> exec (compile1)
0
1
2
3
4
5
6
7
8
9 >>> #简单求值表达式用eval
>>> code2 = '1 + 2 + 3 + 4'
>>> compile2 = compile(code2,'','eval')
>>> eval(compile2)
10 >>> #交互语句用single
>>> code3 = 'name = input("please input your name:")'
>>> compile3 = compile(code3,'','single')
>>> name #执行前name变量不存在
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
name
NameError: name 'name' is not defined
>>> exec(compile3) #执行时显示交互命令,提示输入
please input your name:'pythoner'
>>> name #执行后name变量有值
"'pythoner'"
Python内置函数compile的更多相关文章
- python内置函数-compile()
python的内置函数 compile()--编译. 这个函数有什么用呢? 一个最简单的例子, 就是我们的代码, 会被解释器读取,解释器读取后的其实是字符串, 然后通过compile编译后, 又转换成 ...
- python 内置函数 : compile()
这个函数用来编译一段字符串的源码,结果可以生成字节码或者AST(抽像语法树),字节码可以使用函数exec()来执行,而AST可以使用eval()来继续编译. 参数source是一串字符串的源码,或者是 ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
- python内置函数大全(分类)
python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...
- lambda 表达式+python内置函数
#函数 def f1(a,b): retrun a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda p ...
随机推荐
- pt-online-schema-change的用法
pt-online-schema-change的用法 环境: 10.192.30.53 主库 10.192.30.60 从库 mysql版本:8.0.17 为了方便操作,简单的写了如下的脚本. #!/ ...
- 两句话掌握 Python 最难知识点——元类
千万不要被所谓“元类是99%的python程序员不会用到的特性”这类的说辞吓住.因为每个中国人,都是天生的元类使用者 学懂元类,你只需要知道两句话: 道生一,一生二,二生三,三生万物 我是谁?我从哪来 ...
- zabbix 磁盘自动发现脚本
##需要在zabbix界面配置宏变量===>正则来匹配磁盘 disk_discovery.sh ———————————————————————————————————————————————— ...
- [转] C++ 的关键字(保留字)完整介绍
转载至:https://www.runoob.com/w3cnote/cpp-keyword-intro.html 1. asm asm (指令字符串):允许在 C++ 程序中嵌入汇编代码. 2. a ...
- maven项目使用自己创建的jar包--maven without test code
eclipse版本为2018-12(4.10.0) 1.创建一个jar包 首先自己建立了一个maven project,名为jweb.GAV坐标: <groupId>amberai< ...
- 跨平台迁移数据库windows-Linux
将10.10.1.127服务器的数据库ORCL(WINDOWS)迁移到VM 10.10.10.168LINUX平台 操作系统:Windows server 2008r2 64bit CentOS L ...
- [CSP-S模拟测试]:格式化(贪心)
题目传送门(内部题105) 输入格式 每组数据第一行一个正整数$n$,表示硬盘块数,接下来$n$行,每行两个正整数,第一个正整数为硬盘格式化前的容量,第二个正整数为格式化之后的容量. 输出格式 对每组 ...
- Chrome 66 新增异步剪贴板 API
在过去的几年里我们只能使用 document.execCommand 来操作剪贴板.不过,这种操作剪贴板的操作是同步的,并且只能读取和写入 DOM. 现在 Chrome 66 已经支持了新的 Asyn ...
- 微信小程序 API 网络(ajax)
网络 API 类似于 ajax 向服务器请求网络地址,唯一不同的是这个请求有很多的规则,且必须向服务器上请求,不能在本地请求 网络 发送请求: wx.request() 发起https网络请求 参数: ...
- MyRocks安装部署
参考:https://www.cnblogs.com/WonderHow/p/5621591.html CentOS 7.3 gflags:git clone https://github.com/g ...