Python内置函数(60)——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 flags argument 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. - 将字符串编码成代码或者AST对象,使之能通过 exec 语句来执行或者 eval 来取值
- 说明:
- 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内置函数(60)——compile的更多相关文章
- Python内置函数(12)——compile
英文文档: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) Compile the source i ...
- Python内置函数(60)——staticmethod
英文文档: staticmethod(function) Return a static method for function. A static method does not receive a ...
- lambda 表达式+python内置函数
#函数 def f1(a,b): retrun a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda p ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- 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 ...
随机推荐
- SyntaxHighlighter去掉右上角帮助图标的正确方法
先贴出问题图片: 关于这个问题.网上有很多的帖子,说了三种方法,经过测试,发现其中有些方法是有问题的,有的方法虽然能过解决问题,但是却会带来其他的错误.现在说明如下: 网上的原话: syntaxhig ...
- LOOKUP函数入门
2018-02-11 作者:ExcelHome 阅读:22045次 转载请注明来源"ExcelHome"并保留原文链接.固定链接:http://www.excelh ...
- 在Simplicity Studio下创建适用于EFR32的工程项目
1.使用平台 使用平台描述了在Simplicity Stdio下创建工程时所使用的操作系统与软件版本等. 操作系统:Windows 10 Simplcity Studio版本:Simlicity St ...
- Windows 下安装 swoole 具体步骤
Windows 下安装 swoole 具体步骤: Swoole,原本不支持在Windows下安装的,所以我们要安装Cygwin来使用.在安装Cygwin下遇到了很多坑,百度经验上的文档不是很全,所以我 ...
- Maven-03: 优化依赖
已解析依赖: Maven会自动解析项目的直接依赖和传递性依赖,并且根据规则正确判断每个依赖的范围,对于一些依赖冲突,也能进行调节,以确保任何一个构件只有唯一的版本在依赖中存在.在这些工作之后,最后得到 ...
- 工厂模式的进阶复习(Factory)
工厂模式进阶复习 看了多遍的工厂模式,老是忘记不同模式有什么区别,本文重点说明一下工厂模式的三种方式(简单工厂模式,工厂方法模式,抽象工厂模式)的区别 1.简单工厂模式 简单工厂模式通过Factory ...
- Day4----------用户、群组、权限
一.创建用户 命令:useradd 详细信息: root:x:0:0:root:/root:/bin/bash 总共七位 tom:x:500:500: :/home/tom:/bin/bash 1.r ...
- 【JS】 Javascript与HTML DOM的互动 寻路
JS HTML DOM DOM的全程是Document Object Module,即文档对象模型.一般来说,当一个页面被加载时,浏览器会在内部创建一个当前文档的DOM.就像用python的Etree ...
- 理解HDFS
综述 当数据集的大小超过一台独立的物理计算机的存储能力时,就有必要对它进行分区并存储到若干台单独的计算机上.HDFS是hadoop的主要分布式存储系统,一个HDFS集群主要包括NameNode用来管理 ...
- java基础笔记(4)----数组
介绍: 数组是一种数据类型,是引用类型,是一块连续的内存空间,用于存储和管理相同类型的多个数据. 定义:-- > 数组的声明方式 先声明,在开辟内存空间--> int [] a; a=ne ...