python函数之compile()函数

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

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

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

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

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

立即学习“Python免费学习笔记(深入)”;

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

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

英文说明

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

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.

Changed in version 2.3: The flags and dont_inherit arguments were added.

Changed in version 2.6: Support for compiling AST objects.

Changed in version 2.7: Allowed use of Windows and Mac newlines. Also input in ‘exec’ mode does not have to end in a newline anymore.

代码示例

>>> code = "for i in range(0, 10): print i">>> cmpcode = compile(code, '', 'exec')>>> exec cmpcode0123456789>>> str = "3 * 4 + 5">>> a = compile(str,'','eval')>>> eval(a)17

登录后复制

以上就是python函数之compile()函数的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2266959.html

(0)
上一篇 2025年2月27日 09:42:48
下一篇 2025年2月23日 12:19:17

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • Python装饰器之property()教程详解

    1. 何为装饰器? 官方定义:装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志、性能测试、事务处理等。装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷同代码并继续重用…

    编程技术 2025年2月27日
    200
  • python中select模块的深度解析

    简介 python中的select模块专注于i/o多路复用,提供了select  poll  epoll三个方法(其中后两个在linux中可用,windows仅支持select),另外也提供了kqueue方法(freebsd系统) sele…

    编程技术 2025年2月27日
    200
  • Python文件的读写及文件字符编码设置方法详解

    文件读写操作在各种编程语言中都是比较重要的部分,也是很常用的部分,今天就来详细说一下python对文件的读写操作,以及需要注意的点。 一. python打开文件 代码如下: f = open(“d:est.txt”, “w”) 登录后复制 …

    编程技术 2025年2月27日
    200
  • Python防止sql注入方法介绍

    前言 web漏洞之首莫过于sql了,不管使用哪种语言进行web后端开发,只要使用了关系型数据库,可能都会遇到sql注入攻击问题。那么在python web开发的过程中sql注入是怎么出现的呢,又是怎么去解决这个问题的? 当然,我这里并不想讨…

    编程技术 2025年2月27日
    200
  • python判断视频是否为mp3格式的方法介绍

    项目中使用mp3格式进行音效播放,遇到一个mp3文件在程序中死活播不出声音,最后发现它是wav格式的文件,却以mp3结尾。要对资源进行mp3格式判断,那么如何判断呢,用.mp3后缀肯定不靠谱,我们知道扩展名是可以任意修改的,得从编码格式判断…

    编程技术 2025年2月27日
    200
  • Python装饰器之property用法详解

    @property装饰器能把一个方法变成属性一样来调用,下面我们就一起来看看python黑魔法@property装饰器的使用技巧解析 @property有什么用呢?表面看来,就是将一个方法用属性的方式来访问. 上代码,代码最清晰了. cla…

    编程技术 2025年2月27日
    200
  • python 装饰器

    之前就了解到了装饰器, 但是就会点皮毛, 而且对其调用方式感到迷茫,正好现在的项目我想优化,就想到了用装饰器, 因此深入研究了下装饰器. 先看下代码: import time# 将函数作为参数传入到此方法….def timeif(fun…

    2025年2月27日
    200
  • Python中关于input和raw_input的比较

    这篇文章主要介绍了python中input与raw_input 之间的比较的相关资料,通过本文希望能帮助到大家,对于他们之间的使用方法和区别,需要的朋友可以参考下 Python中input与raw_input 之间的比较 input和raw…

    编程技术 2025年2月27日
    200
  • Python中关于str与repr的使用详解

    这篇文章主要介绍了python 基础教程之str和repr的详解的相关资料,主要说明他们之家的区别,通过此文希望能帮助到大家,帮助大家理解这部分内容,需要的可以参考下 Python str和repr的详解 str可以将值转化为合理的字符串形…

    编程技术 2025年2月27日
    200
  • Python如何实现爬取需要登录的网站代码实例

    这篇文章主要介绍了python实现爬取需要登录的网站,结合完整实例形式分析了python登陆网站及数据抓取相关操作技巧,需要的朋友可以参考下 本文实例讲述了Python爬取需要登录的网站实现方法。分享给大家供大家参考,具体如下: impor…

    编程技术 2025年2月27日
    200

发表回复

登录后才能评论