iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python内置函数3-compile(
  • 561
分享到

python内置函数3-compile(

函数pythoncompile 2023-01-31 07:01:20 561人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

Help on built-in function compile in module __builtin__:compile(...)    compile(source, filename, mode[, flags[, dont_in

Help on built-in function compile in module __builtin__:


compile(...)

    compile(source, filename, mode[, flags[, dont_inherit]]) -> code object

    

    Compile the source string (a python module, statement or expression)

    into a code object that can be executed by the exec statement or eval().

    The filename will be used for run-time error messages.

    The mode must be 'exec' to compile a module, 'single' to compile a

    single (interactive) statement, or 'eval' to compile an expression.

    The flags argument, if present, controls which future statements influence

    the compilation of the code.

    The dont_inherit argument, if non-zero, stops the compilation inheriting

    the effects of any future statements in effect in the code calling

    compile; if absent or zero these statements do influence the compilation,

    in addition to any features explicitly specified.

compile(source, filename, mode[, flags[, dont_inherit]])

Compile the source into a code or AST object. Code objects can be executed by an exec statement or evaluated by a call to eval(). source can either be a Unicode string, a Latin-1 encoded string or an AST object. Refer to the ast module 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 than None will 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_flag attribute on the _Feature instance in the __future__ module.


This function raises SyntaxError if the compiled source is invalid, and TypeError if 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 the code module.


compile(source, filename, mode[, flags[, dont_inherit]])


中文说明:将source编译为代码或者AST对象。代码对象能够通过exec语句来执行或者eval()进行求值。


参数source:字符串或者AST(Abstract Syntax Trees)对象。

参数filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。

参数model:指定编译代码的种类。可以指定为 ‘exec’,’eval’,’single’。

参数flag和dont_inherit:这两个参数暂不介绍,可选参数。


版本:在python2.3、2.6、2.7、3.2中均有不同,使用时要引起注意,兼容python3


>>> code="for i in range(0,10): print i"

>>> cmpcode=compile(code,'','exec')

>>> exec cmpcode

0

1

2

3

4

5

6

7

8

9

>>> str="3*4+5"

>>> a=compile(str,'','eval')

>>> eval(a)

17


--结束END--

本文标题: python内置函数3-compile(

本文链接: https://www.lsjlt.com/news/191615.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python内置函数3-compile(
    Help on built-in function compile in module __builtin__:compile(...)    compile(source, filename, mode[, flags[, dont_in...
    99+
    2023-01-31
    函数 python compile
  • python内置函数3-cmp()
    Help on built-in function cmp in module __builtin__:cmp(...)    cmp(x, y) -> integer        Return negative if x<y...
    99+
    2023-01-31
    函数 python cmp
  • python内置函数3-dir()
    Help on built-in function dir in module __builtin__:dir(...)    dir([object]) -> list of strings        If called wit...
    99+
    2023-01-31
    函数 python dir
  • python内置函数3-complex(
    Help on class complex in module __builtin__:class complex(object) |  complex(real[, imag]) -> complex number |   |  C...
    99+
    2023-01-31
    函数 python complex
  • zero python.3 内置函数
    内置函数...
    99+
    2023-01-31
    函数 python
  • python内置函数3-delattr(
    Help on built-in function delattr in module __builtin__:delattr(...)    delattr(object, name)        Delete a named attr...
    99+
    2023-01-31
    函数 python delattr
  • Python基础3 函数、递归、内置函数
    本节内容1. 函数基本语法及特性2. 参数与局部变量3. 返回值嵌套函数4.递归5.匿名函数6.函数式编程介绍7.高阶函数8.内置函数温故知新1. 集合主要作用: 去重关系测试, 交集\差集\并集\反向(对称)差集>>> ...
    99+
    2023-01-31
    递归 函数 基础
  • Python的compile函数怎么用
    今天小编给大家分享一下Python的compile函数怎么用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。实例>>...
    99+
    2023-06-08
  • Python的compile函数语法是什么
    这篇文章主要介绍“Python的compile函数语法是什么”,在日常操作中,相信很多人在Python的compile函数语法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python的compile函...
    99+
    2023-06-08
  • Python内置数据结构3
    解构In [8]: lst = [1,2] In [9]: lst Out[9]: [1, 2] In [10]: first,second = lst  #解构 In [11]: print(first,second) 1 2按照元...
    99+
    2023-01-31
    数据结构 Python
  • Python函数介绍:compile函数的功能和示例
    Python函数介绍:compile函数的功能和示例一、compile函数的功能在Python中,compile函数是一个内置函数,用于编译源代码为可执行代码或AST对象。它返回一个代码对象,可以被exec或eval语句执行。compile...
    99+
    2023-11-03
    功能 示例 compile函数
  • python 内置函数
    python内置了一系列的常用函数,以便于我们使用python。基本的数据操作基本都是一些数学运算(当然除了加减乘除)、逻辑操作、集合操作、基本IO操作,然后就是对于语言自身的反射操作,还有就是字符串操作。官方文档:https://docs...
    99+
    2023-01-30
    函数 python
  • python内置函数
    什么是内置函数 就是python给你提供的,拿来直接用的函数, 比如print 和 input等等. 截止到python版本3.6.2 python一共提供了68个内置函数. 他们就是python直接提供给我们的,有一些我们已经见过了. ...
    99+
    2023-01-30
    函数 python
  • python学习3-内置数据结构3-by
    一、字符串与bytesstr是文本系列,有编码,bytes是字节系列,没有编码,文本的编码是字符如何用字节来表示。都不可变,python3默认使用utf8。文本转换编码:s.encode(['编码方式'])编码转换文本:s.decode([...
    99+
    2023-01-31
    数据结构 python
  • python 函数(3)
    1. 函数小高级 ( 5* ) 1 函数名可以当作变量来使用 def func(): print(123) v1 = func # func代表函数的地址 func() v1() # v1、func的函数地址相同,执...
    99+
    2023-01-31
    函数 python
  • python 函数3
    函数>>> def ds(x):                         return 2 * x + 1>>> ds(5)11>>> lambda x : 2 * x + 1 ...
    99+
    2023-01-31
    函数 python
  • python学习3-内置数据结构3-字符
    字符串是集合类型1、定义s = 'hello python's = "hellp python"以上2种没有区别s = '''hello python'''s = """hello python"""以上2种没有区别区别在于三引号可以定义多...
    99+
    2023-01-31
    数据结构 字符 python
  • Python的内置函数
    1.什么是内置函数   就是python给你提供的. 拿来直接⽤的函数, 比如print., input等等. 截止到python版本3.6 python一共提供了68个内置函数. 他们就是python直接提供给我们的 Makedo...
    99+
    2023-01-31
    函数 Python
  • Python之内置函数
    ''' 内置函数 :     作用域相关(2) :         locals : 返回当前局部作用域内的所有内容         globals : 返回全局作用域内的所有内容     基础数据类型相关(38) :         和数...
    99+
    2023-01-31
    函数 Python
  • python内置函数1
    1.r=compile(s,"<string>","exec")  compile()将字符串编译成python代码2.exec(r)  执行python代码3.eval("8*6") eval("")里面只能执行表达式,执行e...
    99+
    2023-01-31
    函数 python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作